Use lodash to find a substring from an array of strings

I study lodash. Can lodash be used to find a substring in an array of strings?

var myArray = [ 'I like oranges and apples', 'I hate banana and grapes', 'I find mango ok', 'another array item about fruit' ] 

Can I confirm whether the word "oranges" is in my array? I tried _.includes, _.some, _.indexOf, but they all failed because they look at the full string, not the substring

+5
source share
7 answers

You can easily iterate over some () using higher order lodash functions. For instance:

 _.some(myArray, _.unary(_.partialRight(_.includes, 'orange'))); 

The unary () function ensures that only a callback is passed to only one argument. The partialRight () function is used to apply the value of 'orange' as the second argument to includes () . The first argument is provided with each iteration of some() .

However, this approach will not work if case sensitivity matters. For example, 'orange' will return false. Here you can handle case sensitivity:

 _.some(myArray, _.method('match', /Orange/i)); 

The method () function creates a function that will call the given method of the first argument passed to it. Here we match a case-insensitive regular expression.

Or, if case sensitivity doesn't matter and you just prefer the method() approach, this also works for ES2015:

 _.some(myArray, _.method('includes', 'orange')); 
+12
source

Two quick ways to do this - don't use lodash (sorry)

 var found = myArray.filter(function(el){ return el.indexOf('oranges') > -1; }).length; if (found) { // oranges was found } 

or, as I mentioned in the comment:

 var found = myArray.join(',').indexOf('oranges') > -1; if (found) { // oranges was found } 
+3
source

You can do this with lodash, but it is also very convenient using your own javascript methods:

 function stringArrayContains(array, str) { function contains(el) { return (el.indexOf(str) !== -1) ? true : false; } return array.some(contains); } 

Testing the specified function:

 var a = ['hello', 'there']; var b = ['see', 'ya', 'later']; stringArrayContains(a, 'ell') // true stringArrayContains(a, 'what') // false stringArrayContains(b, 'later') // true stringArrayContains(b, 'hello') // false 

Array.prototype.some applies the function that you define for each element of the array. This function (named contains in our case) should return true or false. Iterating through array elements, if any of the elements returns true, some method returns true.

Personally, I think that in general, if you can use your own JS methods for simple functions, it is preferable to load the library to do the same. Lodash does have performance advantages, but they are not necessarily implemented unless you are processing large amounts of data. Only my two cents.

Hooray!

0
source

The best way is to define a function to check for the inclusion of a substring.

 var contains = _.curry(function (substring, source) { return source.indexOf(substring) !== -1; }); 

I use _.curry here to get the curried function, which can be partially applied then.

 _.some(myArray, contains('item')); 

You can also find the substring in the concatenated string.

 contains('item', _.join(myArray)) 

UPD:

I did not notice that lodash already has a function to search for values โ€‹โ€‹in a collection.

The _.includes function _.includes exactly the same as I defined above. However, like everything in lodash, it uses a different order of arguments. In my example, I put the source as the last argument to the curried function, which makes my function useful for dotless style programming when lodash expects the source to be the first argument to the same function.

Check out the talk of Brian Lonsdorf on this issue https://www.youtube.com/watch?v=m3svKOdZijA

Also consider ramda . This library provides the best way to practice functional programming in JavaScript.

0
source

I came across this question / answer, trying to figure out how to match a substring with each row in an array and DELETE any element of the array containing that substring .

While the answers above put me on the path, and although this does not specifically answer the original question, this thread appears first in a Google search when you are trying to figure out how to perform the aforementioned deletion of an array element so I decided that I would post the answer here.

I ended up finding a way to use the Lodash _.remove function to delete the corresponding array lines as follows:

  // The String (SubString) we want to match against array (for dropping purposes) var searchSubString = "whatever" // Remove all array items that contain the text "whatever" _.remove(my_array, function(searchSubString) { return n.indexOf(searchSubString) !== -1; }); 

Basically, indexOf maps to the position of the substring inside the string, if no substring is found, it will return -1 when indexOf returns a number other than -1 (the number is the SubString position in the number of characters inside the array string).

Lodash deletes this Array element through a mutation in the array, and the newly modified array can be accessed with the same name.

0
source
 _.some(myArray, function(str){ return _.includes(str, 'orange') }) 
-1
source
 let str1 = 'la riviรจre et le lapin sont dans le prรจs'; let str2 = 'product of cooking class'; let str3 = 'another sentence to /^[analyse]/i with weird!$" chars@ '; _.some(_.map(['rabbit','champs'], w => str1.includes(w)), Boolean), // false _.some(_.map(['cook'], w => str2.includes(w)), Boolean), // true _.some(_.map(['analyse'], w => str3.includes(w)), Boolean), // true 
-1
source

Source: https://habr.com/ru/post/1243803/


All Articles