Files
30-seconds-of-code/snippets/indexOfAll.md
Rohit Tanwar c500de403d Update
2018-01-06 16:41:42 +05:30

529 B

indexOfAll

Returns an array of indexes at which the val occurs in arr. If it occurs only once return the index and if it never occurs returns -1

const indexOfAll = (arr, val) => {
  const indices = [];
  arr.forEach((el, i) => el === val && indices.push(i))
  return indices.length ? indices : [-1];
};

indexOfAll([1,2,3],1); // [0]
indexOfAll([1,2,3,1,2,3],1); // [0,3]
indexOfAll([1,2,3],4); // [-1]
indexOfAll([[1,2,3]],[1,2,3]); // [-1] (Array.prototype.indexOf()) has the same behaviour