Merge pull request #669 from Siarhei-Zharnasek/master
Simplify takeWhile
This commit is contained in:
@ -2,12 +2,12 @@
|
||||
|
||||
Removes elements in an array until the passed function returns `true`. Returns the removed elements.
|
||||
|
||||
Loop through the array, using a `for...of` loop over `Array.keys()` until the returned value from the function is `true`.
|
||||
Loop through the array, using a `for...of` loop over `Array.entries()` until the returned value from the function is `true`.
|
||||
Return the removed elements, using `Array.slice()`.
|
||||
|
||||
```js
|
||||
const takeWhile = (arr, func) => {
|
||||
for (let i of arr.keys()) if (func(arr[i])) return arr.slice(0, i);
|
||||
for (const [i, val] of arr.entries()) if (func(val)) return arr.slice(0, i);
|
||||
return arr;
|
||||
};
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user