Update snippet descriptions
This commit is contained in:
@ -3,14 +3,14 @@ title: indexOfAll
|
||||
tags: array,intermediate
|
||||
---
|
||||
|
||||
Returns all indices of `val` in an array.
|
||||
If `val` never occurs, returns `[]`.
|
||||
Returns 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 indices for matching elements.
|
||||
- Return the array of indices.
|
||||
- Use `Array.prototype.reduce()` to loop over elements and store indexes for matching elements.
|
||||
|
||||
```js
|
||||
const indexOfAll = (arr, val) => arr.reduce((acc, el, i) => (el === val ? [...acc, i] : acc), []);
|
||||
const indexOfAll = (arr, val) =>
|
||||
arr.reduce((acc, el, i) => (el === val ? [...acc, i] : acc), []);
|
||||
```
|
||||
|
||||
```js
|
||||
|
||||
Reference in New Issue
Block a user