Update indexOfAll snippet return type
Now we're just going to return an empty array. This keeps a consistent return type, does not need checking, and won't break api chaining. Decision was made because otherwise you'd have to add a filter step or check if the return is not an array. Instead we just want an empty array so it has a consistent API, and no indeces is a valid amount
This commit is contained in:
@ -9,11 +9,11 @@ Return `[-1]` if `length` of the array of indices is `0`, otherwise return the a
|
|||||||
const indexOfAll = (arr, val) => {
|
const indexOfAll = (arr, val) => {
|
||||||
const indices = [];
|
const indices = [];
|
||||||
arr.forEach((el, i) => el === val && indices.push(i));
|
arr.forEach((el, i) => el === val && indices.push(i));
|
||||||
return indices.length ? indices : [-1];
|
return indices;
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
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); // []
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user