// 查找数组中某个元素所有出现的索引
// needle 查找元素 haystack 目标数组
function find(needle, haystack) {
var results = [];
var idx = haystack.indexOf(needle);
while (idx != -1) {
results.push(idx);
idx = haystack.indexOf(needle, idx + 1);
}
return results;
}
// var scores = [10, 20, 30, 10, 40, 20];
// console.log(find(10,scores));
js 工具方法
这是一个JavaScript函数,用于查找数组中指定元素的所有出现索引。它通过indexOf方法迭代搜索,将找到的索引添加到结果数组中。示例中展示了如何在名为scores的数组中查找并返回数字10的所有索引。

被折叠的 条评论
为什么被折叠?



