If you're looking to find the indexes of a specific item in an array, you can achieve this using the reduce method:
const indexesOf = (arr, item) =>
arr.reduce(
(acc, v, i) => (v === item && acc.push(i), acc),
[]);
For example:
const array = ["test234", "test9495", "test234", "test93992", "test234"];
console.log(indexesOf(array, "test234")); // [0, 2, 4]
Another approach is to use an iterator:
function* finder(array, item) {
let index = -1;
while ((index = array.indexOf(item, index + 1)) > -1) {
yield index;
}
return -1;
}
This allows for lazy searching, only retrieving indexes when needed:
let findTest234 = finder(array, "test234");
console.log(findTest234.next()) // {value: 0, done: false}
console.log(findTest234.next()) // {value: 2, done: false}
console.log(findTest234.next()) // {value: 4, done: false}
console.log(findTest234.next()) // {value: -1, done: true}
The iterator can also be used in loops:
let indexes = finder(array, "test234");
for (let index of indexes) {
console.log(index);
}
To generate arrays immediately, you can consume the iterator like so:
let indexes = [...finder(array, "test234")];
console.log(indexes); // [0, 2, 4]
I hope this solution is helpful for your needs.