f



Filter array of objects for property

If I have an array that looks like this

var arr =
[
   {
     a: "foo_2",
     b: "trigger_1",
     c: "bar_3"
   },
   {
     a: "foo_3",
     b: "foo_4",
     c: "bar_4"
   },
   {
     a: "foo_6",
     b: "trigger_5",
     c: "bar_5"
   }
....
];

and to get an array of all "b" values that start with "trigger_" and end 
with a number I'm calling

function getValues(theArr, pre) {
   var i = 0,
       fnd = [],
       tmp;

   for (i = 0; i < theArr.length; i++) {
     tmp = theArr[i].b;
     if ( /trigger_\d+$/.test(tmp) ) {
       fnd.push(tmp);
     }
   }

   return fnd;
}

While this works I'd like the argument "pre" to be a string I pass to 
the regexp so that I can test other values and not just "trigger_" but I 
don't know how.

And secondly is there an easier way to get values for an array of objects?

Andrew Poulos
0
Andrew
10/12/2016 10:44:45 PM
comp.lang.javascript 38370 articles. 2 followers. javascript4 (1315) is leader. Post Follow

4 Replies
477 Views

Similar Articles

[PageSpeed] 8

Andrew Poulos <ap_prog@hotmail.com> writes:

> If I have an array that looks like this
>
> var arr =
> [
>   {
>     a: "foo_2",
>     b: "trigger_1",
>     c: "bar_3"
>   },
>   {
>     a: "foo_3",
>     b: "foo_4",
>     c: "bar_4"
>   },
>   {
>     a: "foo_6",
>     b: "trigger_5",
>     c: "bar_5"
>   }
> ...
> ];
>
> and to get an array of all "b" values that start with "trigger_" and
> end with a number I'm calling
>
> function getValues(theArr, pre) {
>   var i = 0,
>       fnd = [],
>       tmp;
>
>   for (i = 0; i < theArr.length; i++) {
>     tmp = theArr[i].b;
>     if ( /trigger_\d+$/.test(tmp) ) {
>       fnd.push(tmp);
>     }
>   }
>
>   return fnd;
> }
>
> While this works I'd like the argument "pre" to be a string I pass to
> the regexp so that I can test other values and not just "trigger_" but
> I don't know how.

You can construct a RegExp from a string:

  RegExp(pre + '\\d+$').test(tmp)

> And secondly is there an easier way to get values for an array of
> objects?

One way would be to use map and filter.  Combining these with the arrow
notation for functions you could write:

  arr.map(x => x.b).filter(x => RegExp(pre + '\\d+$').test(x))

(ES6 would also permit you to write `${pre}\\d+$`)

-- 
Ben.
0
Ben
10/12/2016 11:27:03 PM
On 13/10/2016 10:27 AM, Ben Bacarisse wrote:
> Andrew Poulos <ap_prog@hotmail.com> writes:
>
>> If I have an array that looks like this
>>
>> var arr =
>> [
>>   {
>>     a: "foo_2",
>>     b: "trigger_1",
>>     c: "bar_3"
>>   },
>>   {
>>     a: "foo_3",
>>     b: "foo_4",
>>     c: "bar_4"
>>   },
>>   {
>>     a: "foo_6",
>>     b: "trigger_5",
>>     c: "bar_5"
>>   }
>> ...
>> ];
>>
>> and to get an array of all "b" values that start with "trigger_" and
>> end with a number I'm calling
>>
>> function getValues(theArr, pre) {
>>   var i = 0,
>>       fnd = [],
>>       tmp;
>>
>>   for (i = 0; i < theArr.length; i++) {
>>     tmp = theArr[i].b;
>>     if ( /trigger_\d+$/.test(tmp) ) {
>>       fnd.push(tmp);
>>     }
>>   }
>>
>>   return fnd;
>> }
>>
>> While this works I'd like the argument "pre" to be a string I pass to
>> the regexp so that I can test other values and not just "trigger_" but
>> I don't know how.
>
> You can construct a RegExp from a string:
>
>   RegExp(pre + '\\d+$').test(tmp)

I missed the double backslash.

>> And secondly is there an easier way to get values for an array of
>> objects?
>
> One way would be to use map and filter.  Combining these with the arrow
> notation for functions you could write:
>
>   arr.map(x => x.b).filter(x => RegExp(pre + '\\d+$').test(x))
>
> (ES6 would also permit you to write `${pre}\\d+$`)

Thanks.

Andrew Poulos

0
Andrew
10/13/2016 1:57:32 AM
Ben Bacarisse wrote:

> Andrew Poulos <ap_prog@hotmail.com> writes:
>> If I have an array that looks like this
>>
>> var arr =
>> [
>>   {
>>     a: "foo_2",
>>     b: "trigger_1",
>>     c: "bar_3"
>>   },
>>   {
>>     a: "foo_3",
>>     b: "foo_4",
>>     c: "bar_4"
>>   },
>>   {
>>     a: "foo_6",
>>     b: "trigger_5",
>>     c: "bar_5"
>>   }
>> ...
>> ];
>>
>> and to get an array of all "b" values that start with "trigger_" and
>> end with a number I'm calling
>>
>> function getValues(theArr, pre) {
>>   var i = 0,
>>       fnd = [],
>>       tmp;
>>
>>   for (i = 0; i < theArr.length; i++) {
>>     tmp = theArr[i].b;
>>     if ( /trigger_\d+$/.test(tmp) ) {
>>       fnd.push(tmp);
>>     }
>>   }
>>
>>   return fnd;
>> }
>>
>> While this works I'd like the argument "pre" to be a string I pass to
>> the regexp so that I can test other values and not just "trigger_" but
>> I don't know how.
> 
> You can construct a RegExp from a string:
> 
>   RegExp(pre + '\\d+$').test(tmp)

First of all, the original regular expression does _not_ match strings that 
*start* with “trigger_”; it matches all those that *contain* “trigger_” 
followed by a sequence of decimal digits, because the “^” anchor is missing.

Second, *in general*, string values need to be escaped if they are used in 
regular expressions to match substrings literally.  For example, “.” needs 
to be escaped as ”\.” to match a literal “.” character; otherwise it would 
match any non-newline character.  My jsx.regexp.escape() and 
String.prototype.regExpEscape() methods provide this functionality: you can 
pass a string and it returns a pattern string that, when passed to the 
RegExp constructor, matches the original string. [1]

However, *in this case* it might not be necessary to escape the pattern 
string because the examples do not contain RegExp-special characters.

Third, it is, *in general*, not necessary to use regular expressions for 
both parts of the pattern, therefore not necessary to escape the variable 
part.  The test can consist of two parts: one that tests whether the string 
starts with a certain substring, and another that tests whether it ends with 
a sequence of decimal digits:

  if (tmp.indexOf(pre) == 0 && /\d+$/.test(tmp))
  {
    // …
  }

However, note that although the two-part test may suffice *here*, is *not 
equivalent* to the original one because it also would find strings of the 
form "foo_bar3" if pre == "foo_".


[1] <http://PointedEars.de/wsvn/JSX/trunk/regexp.js>
-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.
0
Thomas
10/13/2016 10:21:15 AM
It was the best of times, it was the worst of times when someone called
Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote the following on a
fine sunny Thursday in October:

> Ben Bacarisse wrote:
<snip>
>> You can construct a RegExp from a string:
>> 
>>   RegExp(pre + '\\d+$').test(tmp)
>
> First of all, the original regular expression does _not_ match strings that 
> *start* with “trigger_”; it matches all those that *contain* “trigger_” 
> followed by a sequence of decimal digits, because the “^” anchor is
> missing.

Yes.  Have you considered switching to answering questions?  You'd be
good at it.

<snip more good stuff that I happened to know>
-- 
Ben.
0
Bunny
10/13/2016 12:28:41 PM
Reply: