use Array.slice instead of Array.shift
This commit is contained in:
@ -2,12 +2,12 @@
|
|||||||
|
|
||||||
Removes elements in an array until the passed function returns `true`. Returns the remaining elements in the array.
|
Removes elements in an array until the passed function returns `true`. Returns the remaining elements in the array.
|
||||||
|
|
||||||
Loop through the array, using `Array.shift()` to drop the first element of the array until the returned value from the function is `true`.
|
Loop through the array, using `Array.slice()` to drop the first element of the array until the returned value from the function is `true`.
|
||||||
Returns the remaining elements.
|
Returns the remaining elements.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const dropElements = (arr, func) => {
|
const dropElements = (arr, func) => {
|
||||||
while (arr.length > 0 && !func(arr[0])) arr.shift();
|
while (arr.length > 0 && !func(arr[0])) arr = arr.slice(1);
|
||||||
return arr;
|
return arr;
|
||||||
};
|
};
|
||||||
// dropElements([1, 2, 3, 4], n => n >= 3) -> [3,4]
|
// dropElements([1, 2, 3, 4], n => n >= 3) -> [3,4]
|
||||||
|
|||||||
Reference in New Issue
Block a user