Update snippet descriptions

This commit is contained in:
Isabelle Viktoria Maciohsek
2020-10-20 23:02:01 +03:00
parent 52880f08ee
commit caa67e2a49
63 changed files with 175 additions and 126 deletions

View File

@ -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