在字符串数组中查找字符串

时间:2018-12-02 05:55:02

标签: javascript arrays string

我有一个字符串(名称)数组,例如:

name =["John Doe","Lutfur Kabir", "Moshiur Imtiaz Rahman", "Clark Kent","Jenny Doe"]

我想获取其中包含Doe的名称的索引。如何使用JavaScript进行操作。

2 个答案:

答案 0 :(得分:0)

您可以使用.reduce()创建一个数组,该数组的字符串索引包含所需的字符串:

Collection#toArray(T[])

答案 1 :(得分:0)

您可以使用Array.prototype.includes

Find the previous answer here already

var categoriesPresent = ['word', 'word', 'specialword', 'word'];
var categoriesNotPresent = ['word', 'word', 'word'];

var foundPresent = categoriesPresent.includes('specialword');
var foundNotPresent = categoriesNotPresent.includes('specialword');

console.log(foundPresent, foundNotPresent); // true false