在 JavaScript 中查找数组中单词的所有出现位置
我们需要编写一个 JavaScript 函数,该函数将文字数组作为第一个参数,将字符串作为第二个参数。我们的函数应该返回该字符串(由第二个参数提供)在数组中任意位置出现的次数。
示例
代码如下 −
const arr = ["word", "a word", "another word"];
const query = "word";
const findAll = (arr, query) => {
let count = 0;
count = arr.filter(el => {
return el.indexOf(query) != -1;
}).length;
return count;
};
console.log(findAll(arr, query));输出
控制台中的输出如下 −
3
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP