Update indexOfAll.md
This commit is contained in:
@ -1,6 +1,9 @@
|
|||||||
### indexOfAll
|
### 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 all indices of `val` in an array. If `val` never occurs, returns `[-1]`.
|
||||||
|
|
||||||
|
Use `Array.forEach()` to loop over elements and `Array.push()` to store indices for matching elements.
|
||||||
|
Return `[-1]` if `length` of the array of indices is `0`, otherwise return the array of indices.
|
||||||
|
|
||||||
``` js
|
``` js
|
||||||
const indexOfAll = (arr, val) => {
|
const indexOfAll = (arr, val) => {
|
||||||
@ -10,9 +13,8 @@ const indexOfAll = (arr, val) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
``` js
|
``` js
|
||||||
indexOfAll([1,2,3],1); // [0]
|
|
||||||
indexOfAll([1,2,3,1,2,3],1); // [0,3]
|
indexOfAll([1,2,3,1,2,3],1); // [0,3]
|
||||||
indexOfAll([1,2,3],4); // [-1]
|
indexOfAll([1,2,3],4); // [-1]
|
||||||
indexOfAll([[1,2,3]],[1,2,3]); // [-1] (Array.prototype.indexOf()) has the same behaviour
|
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user