Finds all indexes of val
in an array.
If val
never occurs, returns an empty array.
- Use
Array.prototype.reduce()
to loop over elements and store indexes for matching elements.
代码实现
const indexOfAll = (arr, val) =>
arr.reduce((acc, el, i) => (el === val ? [...acc, i] : acc), []);
indexOfAll([1, 2, 3, 1, 2, 3], 1); // [0, 3]
indexOfAll([1, 2, 3], 4); // []
翻译自:https://www.30secondsofcode.org/js/s/index-of-all-matches