Add takeWhile, takeRightWhile
This commit is contained in:
18
snippets/takeRightWhile.md
Normal file
18
snippets/takeRightWhile.md
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
### takeRightWhile
|
||||||
|
|
||||||
|
Removes elements from the end of 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`.
|
||||||
|
Return the removed elements, using `Array.reverse()` and `Array.slice()`.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const takeRightWhile = (arr, func) => {
|
||||||
|
for (let i of arr.reverse().keys())
|
||||||
|
if (func(arr[i])) return arr.reverse().slice(arr.length - i,arr.length);
|
||||||
|
return arr;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
takeRightWhile([1, 2, 3, 4], n => n < 3); // [3, 4]
|
||||||
|
```
|
||||||
18
snippets/takeWhile.md
Normal file
18
snippets/takeWhile.md
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
### takeWhile
|
||||||
|
|
||||||
|
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`.
|
||||||
|
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);
|
||||||
|
return arr;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
takeWhile([1, 2, 3, 4], n => n >= 3); // [1, 2]
|
||||||
|
```
|
||||||
@ -219,6 +219,8 @@ symmetricDifferenceWith:array,function
|
|||||||
tail:array
|
tail:array
|
||||||
take:array
|
take:array
|
||||||
takeRight:array
|
takeRight:array
|
||||||
|
takeRightWhile:array,function
|
||||||
|
takeWhile:array,function
|
||||||
times:function
|
times:function
|
||||||
timeTaken:utility
|
timeTaken:utility
|
||||||
toCamelCase:string,regexp
|
toCamelCase:string,regexp
|
||||||
|
|||||||
Reference in New Issue
Block a user