Rename indexOfAll

This commit is contained in:
Rohit Tanwar
2018-01-06 15:37:56 +05:30
committed by GitHub
parent 8159db579d
commit b441379b84

View File

@ -1,17 +1,17 @@
### indexesOf ### 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` 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`
``` js ``` js
const indexesOf = (arr, val) => { const indexOfAll = (arr, val) => {
let indexes = [], i; let indexes = [], i;
arr.forEach((el,i) => {if(el === val) indexes.push(i)}) arr.forEach((el,i) => {if(el === val) indexes.push(i)})
return indexes.length === 0 ? [-1] : indexes return indexes.length === 0 ? [-1] : indexes
} }
``` ```
``` js ``` js
indexesOf([1,2,3],1); // [0] indexOfAll([1,2,3],1); // [0]
indexesOf([1,2,3,1,2,3],1); // [0,3] indexOfAll([1,2,3,1,2,3],1); // [0,3]
indexesOf([1,2,3],4); // [-1] indexOfAll([1,2,3],4); // [-1]
indexesOf([[1,2,3]],[1,2,3]); // [-1] (Array.prototype.indexOf()) has the same behaviour indexOfAll([[1,2,3]],[1,2,3]); // [-1] (Array.prototype.indexOf()) has the same behaviour
``` ```