Match one of the array elements in a string using javascript

I am trying to create a function that returns true if at least one of the elements of the string array is found on another string.

function findInString(str) { var fruits = ["orange", "banana", "grape"]; for(var i = 0; i < fruits.length; i++) { if (str.indexOf(fruits[i]) > -1) { return true; } } return false; } var a = findInString("I love orange juice."); //=> returns true var b = findInString("I don't like peach."); //=> returns false 

This function does the trick, but I'm sure there might be some kind of array or string method that does the same thing without looping the array. Any ideas?

Thanks.

+5
source share
6 answers

Conceptually, I cannot think of a better (or even other) way to do this. I do not know about the built-in function that performs this task, but even if it were implemented in any case.

0
source

You can use some , which is very close to this. Here's how I write it:

 function find (str, arr) { return arr.some((s) => s.indexOf(str) > -1); } 

You can do this if you want, but I do not feel well.

 function find (str, arr) { return arr.join(',').indexOf(str) > -1 } 
+1
source

I like your way of doing it. I delved into this a bit, here are a few ways you can think about it:

Some of them are really close, but for the last period some parsing of strings for each case may be required. Pay attention to the latter, since it uses RegExp, it does not require anything to be done for the string:

JsBin example

 function findInString(str) { var fruits = ["orange", "banana", "grape"]; return str.split(' ').filter(function(el) { return fruits.indexOf(el) > -1; }).length > 0; } function finderWithReduce(str) { var fruits = ["orange", "banana", "grape"]; var result = false; str.split(' ').reduce(function(a, b) { if (a.indexOf(b) > -1) { result = true; } return a; }, fruits); return result; } function finderWithForEach(str){ var fruits = ["orange", "banana", "grape"]; var result = false; fruits.forEach(function(fruit) { if (str.indexOf(fruit) > -1) { result = true; } }); return result; } function finderWithRegex(str) { var fruits = ["orange", "banana", "grape"]; for (var i = 0; i < fruits.length; i++) { var re = new RegExp(fruits[i], 'gi'); if (str.match(re) !== null) { return true; } } return false; } 
+1
source

You can use some function.

 function findInString(str, arr) { return arr.some(function(el) { return str.indexOf(el) > -1; }); } var fruits = ["orange", "banana", "grape"]; var a = findInString("I love orange juice.", fruits); //=> returns true var b = findInString("I don't like peach.", fruits); //=> returns false 
+1
source

I think you need to process the array. I would make two changes.

First, I would pass an array, as well as a string, creating a common function, then I would redo it so that as soon as it finds it, it will stop doing this and exit the loop; a similar concept as returning true, but just another way to do this is my personal preference for only one function output.

 function findInString(arr, str) { var hasString = false; for (var i = 0; i < fruits.length; i++) { if (str.indexOf(fruits[i]) > -1) { hasString = true; break; } } return hasString; } var fruits = ["orange", "banana", "grape"]; var a = findInString(fruits, "I love orange juice."); //=> returns true var b = findInString(fruits, "I don't like peach."); //=> returns false 
+1
source

Here is the ES6 functional method. presentIn is a higher order function that takes a string and returns a function that acts as a some callback.

 const presentIn = (str) => (el) => str.includes(el); fruits.some(presentIn('I love orange juice.')); // true fruits.some(presentIn('I don\'t like peach.')); // false 

I really like this approach because you work directly with the elements of the array, and if you call your function well, it brilliantly scans: "Are some elements of the array present in the string."

Demo

A slightly more detailed version of ES5 for comparison:

 function presentIn(str) { return function (el) { return str.indexOf(el) > -1; } } 
+1
source

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


All Articles