使用javascript匹配字符串中数组的一个元素

时间:2021-11-11 19:24:14

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

我正在尝试创建一个函数,如果在另一个字符串中找到字符串数组的至少一个元素,则该函数返回true。

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 some array or string method that does the same without having to loop through the array. Any ideas?

这个函数可以解决这个问题,但我确信可能有一些数组或字符串方法可以执行相同操作而无需遍历数组。有任何想法吗?

Thanks.

6 个解决方案

#1


0  

Conceptually I can't think of a better (or even different) way of doing this. I don't know of a built in function which performs this task but even if there was one it's implementation would be this anyway.

从概念上讲,我无法想到更好(甚至不同)的做法。我不知道执行此任务的内置函数,但即使有一个它的实现也是如此。

#2


1  

You could use some which comes very close to this. Here's how I'd write it:

你可以使用一些非常接近这一点。这是我写的方式:

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

You could do this as well if you'd like but I don't feel good about it.

如果你愿意,你也可以这样做,但我感觉不太好。

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

#3


1  

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

#4


1  

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

我认为你必须处理数组。我会做两个改变。

First I would pass in the array as well as the string, making a generic function, then I would rework it so that once it finds one it quits doing that and exits the loop; similar concept as the return true but just a differing way to do it - my personal preference to only have one function exit.

首先,我将传入数组以及字符串,制作一个泛型函数,然后我会重做它,以便一旦找到它就退出并退出循环;类似的概念,因为返回true,但只是一种不同的方式 - 我个人的偏好只有一个函数退出。

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

#5


1  

I like your way of doing it. I got a little into it, here are several ways you can think about doing this:

我喜欢你这样做的方式。我对此有所了解,这里有几种方法可以考虑这样做:

Some of these are really close, but that last period might require some string parsing to handle every case. Note the last one, since it uses RegExp, wont require doing anything to the string:

其中一些非常接近,但最后一段时间可能需要一些字符串解析来处理每种情况。注意最后一个,因为它使用RegExp,不需要对字符串做任何事情:

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;
}

#6


1  

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

这是一个功能强大的ES6方法。 presentIn是一个高阶函数,它接受一个字符串并返回一个充当某个回调的函数。

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're operating directly on the array elements, and if you name your function well it scans brilliantly: "Are some elements of the array present in the string".

我真的很喜欢这种方法,因为你直接在数组元素上操作,如果你很好地命名你的函数,它会很好地扫描:“数组中的某些元素是否存在于字符串中”。

DEMO

The slightly more verbose ES5 version for comparison:

用于比较的稍微详细的ES5版本:

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

#1


0  

Conceptually I can't think of a better (or even different) way of doing this. I don't know of a built in function which performs this task but even if there was one it's implementation would be this anyway.

从概念上讲,我无法想到更好(甚至不同)的做法。我不知道执行此任务的内置函数,但即使有一个它的实现也是如此。

#2


1  

You could use some which comes very close to this. Here's how I'd write it:

你可以使用一些非常接近这一点。这是我写的方式:

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

You could do this as well if you'd like but I don't feel good about it.

如果你愿意,你也可以这样做,但我感觉不太好。

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

#3


1  

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

#4


1  

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

我认为你必须处理数组。我会做两个改变。

First I would pass in the array as well as the string, making a generic function, then I would rework it so that once it finds one it quits doing that and exits the loop; similar concept as the return true but just a differing way to do it - my personal preference to only have one function exit.

首先,我将传入数组以及字符串,制作一个泛型函数,然后我会重做它,以便一旦找到它就退出并退出循环;类似的概念,因为返回true,但只是一种不同的方式 - 我个人的偏好只有一个函数退出。

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

#5


1  

I like your way of doing it. I got a little into it, here are several ways you can think about doing this:

我喜欢你这样做的方式。我对此有所了解,这里有几种方法可以考虑这样做:

Some of these are really close, but that last period might require some string parsing to handle every case. Note the last one, since it uses RegExp, wont require doing anything to the string:

其中一些非常接近,但最后一段时间可能需要一些字符串解析来处理每种情况。注意最后一个,因为它使用RegExp,不需要对字符串做任何事情:

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;
}

#6


1  

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

这是一个功能强大的ES6方法。 presentIn是一个高阶函数,它接受一个字符串并返回一个充当某个回调的函数。

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're operating directly on the array elements, and if you name your function well it scans brilliantly: "Are some elements of the array present in the string".

我真的很喜欢这种方法,因为你直接在数组元素上操作,如果你很好地命名你的函数,它会很好地扫描:“数组中的某些元素是否存在于字符串中”。

DEMO

The slightly more verbose ES5 version for comparison:

用于比较的稍微详细的ES5版本:

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