Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #31566 > unrolled thread
| Started by | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| First post | 2016-10-13 09:44 +1100 |
| Last post | 2016-10-13 13:28 +0100 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.javascript
Filter array of objects for property Andrew Poulos <ap_prog@hotmail.com> - 2016-10-13 09:44 +1100
Re: Filter array of objects for property Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-10-13 00:27 +0100
Re: Filter array of objects for property Andrew Poulos <ap_prog@hotmail.com> - 2016-10-13 12:57 +1100
Re: Filter array of objects for property Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-10-13 11:21 +0100
Re: Filter array of objects for property Bunny Bocarisse <ben.usenet@bsb.me.uk> - 2016-10-13 13:28 +0100
| From | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| Date | 2016-10-13 09:44 +1100 |
| Subject | Filter array of objects for property |
| Message-ID | <IKWdnYrudsxAIWPKnZ2dnUU7-XXNnZ2d@westnet.com.au> |
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
[toc] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2016-10-13 00:27 +0100 |
| Message-ID | <877f9d87y0.fsf@bsb.me.uk> |
| In reply to | #31566 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| Date | 2016-10-13 12:57 +1100 |
| Message-ID | <rYadnaiHPtSXd2PKnZ2dnUU7-K2dnZ2d@westnet.com.au> |
| In reply to | #31567 |
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
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2016-10-13 11:21 +0100 |
| Message-ID | <1770295.oMNUckLgyt@PointedEars.de> |
| In reply to | #31567 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Bunny Bocarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2016-10-13 13:28 +0100 |
| Message-ID | <87pon477ra.fsf@bsb.me.uk> |
| In reply to | #31569 |
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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web