Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #38727 > unrolled thread

Find part match in array

Started byAndrew Poulos <ap_prog@hotmail.com>
First post2020-08-17 10:56 +1000
Last post2020-08-18 21:59 +0200
Articles 11 — 6 participants

Back to article view | Back to comp.lang.javascript


Contents

  Find part match in array Andrew Poulos <ap_prog@hotmail.com> - 2020-08-17 10:56 +1000
    Re: Find part match in array Andrew Poulos <ap_prog@hotmail.com> - 2020-08-17 13:06 +1000
    Re: Find part match in array "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2020-08-16 20:54 -0700
    Re: Find part match in array JJ <jj4public@vfemail.net> - 2020-08-17 16:36 +0700
    Re: Find part match in array Julio Di Egidio <julio@diegidio.name> - 2020-08-17 05:01 -0700
    Re: Find part match in array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2020-08-17 19:36 +0200
      Re: Find part match in array Andrew Poulos <ap_prog@hotmail.com> - 2020-08-18 09:02 +1000
        Re: Find part match in array Andrew Poulos <ap_prog@hotmail.com> - 2020-08-18 13:09 +1000
        Re: Find part match in array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2020-08-18 11:28 +0200
          Re: Find part match in array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2020-08-18 12:59 +0200
        Re: Find part match in array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2020-08-18 21:59 +0200

#38727 — Find part match in array

FromAndrew Poulos <ap_prog@hotmail.com>
Date2020-08-17 10:56 +1000
SubjectFind part match in array
Message-ID<9rCdnYySpppZSKTCnZ2dnUU7-N3NnZ2d@westnet.com.au>
If I have this

let arr1 = ["Today", "Freddy", "was", "here"];

what's a straightforward way to find that there's a match with this array
   arr2 = ["Fred", "Yesterday"];
(the first element "matches" the second element in arr1 ie 'Fred' is 
within 'Freddy')?

I started with
const matched = arr1.some(a => arr2.includes(a));
but I'm having trouble replacing the "includes" with a regexp and it to 
not error.

Andrew Poulos

[toc] | [next] | [standalone]


#38728

FromAndrew Poulos <ap_prog@hotmail.com>
Date2020-08-17 13:06 +1000
Message-ID<BcSdnU1wiqvTaaTCnZ2dnUU7-TXNnZ2d@westnet.com.au>
In reply to#38727
On 17/08/2020 10:56 am, Andrew Poulos wrote:
> If I have this
> 
> let arr1 = ["Today", "Freddy", "was", "here"];
> 
> what's a straightforward way to find that there's a match with this array
>    arr2 = ["Fred", "Yesterday"];
> (the first element "matches" the second element in arr1 ie 'Fred' is 
> within 'Freddy')?
> 
> I started with
> const matched = arr1.some(a => arr2.includes(a));
> but I'm having trouble replacing the "includes" with a regexp and it to 
> not error.

I gave up and "solved" it with nested for loops...

Andrew Poulos

[toc] | [prev] | [next] | [standalone]


#38729

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2020-08-16 20:54 -0700
Message-ID<3add9209-9027-4b1c-bc4f-4321d1c3ebffn@googlegroups.com>
In reply to#38727
On Sunday, August 16, 2020 at 7:56:45 PM UTC-5, Andrew Poulos wrote:
> If I have this 
> 
> let arr1 = ["Today", "Freddy", "was", "here"]; 
> 
> what's a straightforward way to find that there's a match with this array 
> arr2 = ["Fred", "Yesterday"]; 
> (the first element "matches" the second element in arr1 ie 'Fred' is 
> within 'Freddy')? 
> 
> I started with 
> const matched = arr1.some(a => arr2.includes(a)); 
> but I'm having trouble replacing the "includes" with a regexp and it to 
> not error. 

Maybe something like:

```
const matched = arr1.some(a => 
    arr2.some(b => myRegExp.test(b))
)
```

[toc] | [prev] | [next] | [standalone]


#38730

FromJJ <jj4public@vfemail.net>
Date2020-08-17 16:36 +0700
Message-ID<y00znyb0nmyt$.503doqjd1vp3$.dlg@40tude.net>
In reply to#38727
On Mon, 17 Aug 2020 10:56:34 +1000, Andrew Poulos wrote:
> If I have this
> 
> let arr1 = ["Today", "Freddy", "was", "here"];
> 
> what's a straightforward way to find that there's a match with this array
>    arr2 = ["Fred", "Yesterday"];
> (the first element "matches" the second element in arr1 ie 'Fred' is 
> within 'Freddy')?
> 
> I started with
> const matched = arr1.some(a => arr2.includes(a));
> but I'm having trouble replacing the "includes" with a regexp and it to 
> not error.
> 
> Andrew Poulos

Or...

(new RegExp(arr2.join("|"))).test(arr1.join(","))

[toc] | [prev] | [next] | [standalone]


#38732

FromJulio Di Egidio <julio@diegidio.name>
Date2020-08-17 05:01 -0700
Message-ID<b12e73f4-32ba-4ecc-acdc-e11a2f924f80o@googlegroups.com>
In reply to#38727
On Monday, 17 August 2020 02:56:45 UTC+2, Andrew Poulos  wrote:
> If I have this
> 
> let arr1 = ["Today", "Freddy", "was", "here"];
> 
> what's a straightforward way to find that there's a match with this array
>    arr2 = ["Fred", "Yesterday"];
> (the first element "matches" the second element in arr1 ie 'Fred' is 
> within 'Freddy')?

"Matches/is within" as in *contains*?  Otherwise what?

One of the two arrays is fixed (constant) and you match the other
against it?  Or, both arrays are dynamic and you need to cross-match
them?

> const matched = arr1.some(a => arr2.includes(a));

Besides asking sloppy questions, do you use const vs let
(vs var, and functions vs lambdas) just randomly?  Or is
it on purpose to make the code (even more) unreadable?

Julio

[toc] | [prev] | [next] | [standalone]


#38734

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2020-08-17 19:36 +0200
Message-ID<1927146.oMNUckLgyt@PointedEars.de>
In reply to#38727
Andrew Poulos wrote:

> If I have this
> 
> let arr1 = ["Today", "Freddy", "was", "here"];
> 
> what's a straightforward way to find that there's a match with this array
>    arr2 = ["Fred", "Yesterday"];
> (the first element "matches" the second element in arr1 ie 'Fred' is
> within 'Freddy')?
> 
> I started with
> const matched = arr1.some(a => arr2.includes(a));
> but I'm having trouble replacing the "includes" with a regexp and it to
> not error.

To arrive at a solution, it helps to think about the problem figuratively 
and to give your variables more meaningful names.  It also makes your code 
easier to understand later:

  let haystack = ["Today", "Freddy", "was", "here"];
  let needles = ["Fred", "Yesterday"];

  /* true if any of the needles was found in the haystack */
  const found = needles.some(needle => {
    /* Return true if the needle was found inside a straw of the haystack */
    return haystack.some(straw => (new RegExp(needle)).test(straw));
  });

Notice that there is a difference as to whether you match the contents of 
the second array against the contents of the first one or vice-versa.

Also notice that

  1. You do not have to use regular expressions.  Especially in ECMAScript
     Ed. 6 and newer, there are methods like String.prototype.includes()
     which with you can search for substrings easily.

  2. If you use regular expressions, the needle should be escaped if you
     want to search for a string verbatim instead of matching a pattern
     against an element:

       … new RegExp(needle.replace(/[\]\\^$*+?.(){}|[]/g, "\\$&")) …

     See also
     <https://github.com/PointedEars/JSX/blob/master/regexp.js#L1441> p.
 
-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#38735

FromAndrew Poulos <ap_prog@hotmail.com>
Date2020-08-18 09:02 +1000
Message-ID<qumdnS2Bv-bgkabCnZ2dnUU7-fvNnZ2d@westnet.com.au>
In reply to#38734
On 18/08/2020 3:36 am, Thomas 'PointedEars' Lahn wrote:
> Andrew Poulos wrote:
> 
>> If I have this
>>
>> let arr1 = ["Today", "Freddy", "was", "here"];
>>
>> what's a straightforward way to find that there's a match with this array
>>     arr2 = ["Fred", "Yesterday"];
>> (the first element "matches" the second element in arr1 ie 'Fred' is
>> within 'Freddy')?
>>
>> I started with
>> const matched = arr1.some(a => arr2.includes(a));
>> but I'm having trouble replacing the "includes" with a regexp and it to
>> not error.
> 
> To arrive at a solution, it helps to think about the problem figuratively
> and to give your variables more meaningful names.  It also makes your code
> easier to understand later:
> 
>    let haystack = ["Today", "Freddy", "was", "here"];
>    let needles = ["Fred", "Yesterday"];
> 
>    /* true if any of the needles was found in the haystack */
>    const found = needles.some(needle => {
>      /* Return true if the needle was found inside a straw of the haystack */
>      return haystack.some(straw => (new RegExp(needle)).test(straw));
>    });
> 
> Notice that there is a difference as to whether you match the contents of
> the second array against the contents of the first one or vice-versa.
> 
> Also notice that
> 
>    1. You do not have to use regular expressions.  Especially in ECMAScript
>       Ed. 6 and newer, there are methods like String.prototype.includes()
>       which with you can search for substrings easily.

Using String.prototype.includes is always case sensitive so if I need a 
case insensitive test I'm going to need to convert the strings to, say, 
lower case.

>    2. If you use regular expressions, the needle should be escaped if you
>       want to search for a string verbatim instead of matching a pattern
>       against an element:
> 
>         … new RegExp(needle.replace(/[\]\\^$*+?.(){}|[]/g, "\\$&")) …

This is good to know.

>       See also
>       <https://github.com/PointedEars/JSX/blob/master/regexp.js#L1441> p.

Andrew Poulos

[toc] | [prev] | [next] | [standalone]


#38736

FromAndrew Poulos <ap_prog@hotmail.com>
Date2020-08-18 13:09 +1000
Message-ID<iaSdnd-xZ-r326bCnZ2dnUU7-RPNnZ2d@westnet.com.au>
In reply to#38735
On 18/08/2020 9:02 am, Andrew Poulos wrote:
> On 18/08/2020 3:36 am, Thomas 'PointedEars' Lahn wrote:

>>    1. You do not have to use regular expressions.  Especially in 
>> ECMAScript
>>       Ed. 6 and newer, there are methods like String.prototype.includes()
>>       which with you can search for substrings easily.
> 
> Using String.prototype.includes is always case sensitive so if I need a 
> case insensitive test I'm going to need to convert the strings to, say, 
> lower case.

You were right. Using String.prototype.includes here proved to be less 
problematic.

Andrew Poulos

[toc] | [prev] | [next] | [standalone]


#38737

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2020-08-18 11:28 +0200
Message-ID<XnsAC1D74C09A3AFeejj99@194.109.6.166>
In reply to#38735
Andrew Poulos <ap_prog@hotmail.com> wrote on 18 Aug 2020 in 
comp.lang.javascript:

> Using String.prototype.includes is always case sensitive so if I need a 
> case insensitive test I'm going to need to convert the strings to, say, 
> lower case.
> 

Object.defineProperty( 
	String.prototype, 
	'contains', 
	{
		value: function(p)
			{ return this.toLowerCase().includes(p.toLowerCase()); }
	} 
);

console.log( 'aaa'.contains( 'a' ) ); // true
console.log( 'aaa'.contains( 'A' ) ); // true
console.log( 'aaa'.contains( 'B' ) ); // false


-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#38738

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2020-08-18 12:59 +0200
Message-ID<XnsAC1D843071097eejj99@194.109.6.166>
In reply to#38737
"Evertjan." <exxjxw.hannivoort@inter.nl.net> wrote on 18 Aug 2020 in 
comp.lang.javascript:

> Andrew Poulos <ap_prog@hotmail.com> wrote on 18 Aug 2020 in 
> comp.lang.javascript:
> 
>> Using String.prototype.includes is always case sensitive so if I need a 
>> case insensitive test I'm going to need to convert the strings to, say, 
>> lower case.
>> 
> 
> Object.defineProperty( 
>      String.prototype, 
>      'contains', 
>      {
>           value: function(p)
>                { return this.toLowerCase().includes(p.toLowerCase()); }
>     } 
> );
> 
> console.log( 'aaa'.contains( 'a' ) ); // true
> console.log( 'aaa'.contains( 'A' ) ); // true
> console.log( 'aaa'.contains( 'B' ) ); // false

Simpler:

String.prototype['contains'] = function(p)
	{ return this.toLowerCase().includes(p.toLowerCase()); };

console.log( 'aaa'.contains( 'a' ) ); // true
console.log( 'aaa'.contains( 'A' ) ); // true
console.log( 'aaa'.contains( 'B' ) ); // false






-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#38739

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2020-08-18 21:59 +0200
Message-ID<5407140.DvuYhMxLoT@PointedEars.de>
In reply to#38735
Andrew Poulos wrote:

> On 18/08/2020 3:36 am, Thomas 'PointedEars' Lahn wrote:
>>    1. You do not have to use regular expressions.  Especially in
>>    ECMAScript
>>       Ed. 6 and newer, there are methods like String.prototype.includes()
>>       which with you can search for substrings easily.
> 
> Using String.prototype.includes is always case sensitive so if I need a
> case insensitive test I'm going to need to convert the strings to, say,
> lower case.

Yes, and that can be a problem if you have letters beyond the BMP.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web