Update takeWhile snippets
Add takeUntil snippets
This commit is contained in:
24
snippets/takeRightUntil.md
Normal file
24
snippets/takeRightUntil.md
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
---
|
||||||
|
title: takeRightUntil
|
||||||
|
tags: array,intermediate
|
||||||
|
---
|
||||||
|
|
||||||
|
Removes elements from the end of an array until the passed function returns `true`.
|
||||||
|
Returns the removed elements.
|
||||||
|
|
||||||
|
- Create a reversed copy of the array, using the spread operator (`...`) and `Array.prototype.reverse()`.
|
||||||
|
- Loop through the reversed copy, using a `for...of` loop over `Array.prototype.entries()` until the returned value from the function is truthy.
|
||||||
|
- Return the removed elements, using `Array.prototype.slice()`.
|
||||||
|
- The callback function, `fn`, accepts a single argument which is the value of the element.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const takeRightUntil = (arr, fn) => {
|
||||||
|
for (const [i, val] of [...arr].reverse().entries())
|
||||||
|
if (fn(val)) return i === 0 ? [] : arr.slice(-i);
|
||||||
|
return arr;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
takeRightUntil([1, 2, 3, 4], n => n < 3); // [3, 4]
|
||||||
|
```
|
||||||
@ -3,16 +3,22 @@ title: takeRightWhile
|
|||||||
tags: array,intermediate
|
tags: array,intermediate
|
||||||
---
|
---
|
||||||
|
|
||||||
Removes elements from the end of an array until the passed function returns `true`.
|
Removes elements from the end of an array until the passed function returns `false`.
|
||||||
Returns the removed elements.
|
Returns the removed elements.
|
||||||
|
|
||||||
- Loop through the array, using `Array.prototype.reduceRight()` and accumulating elements while `func` returns falsy values.
|
- Create a reversed copy of the array, using the spread operator (`...`) and `Array.prototype.reverse()`.
|
||||||
|
- Loop through the reversed copy, using a `for...of` loop over `Array.prototype.entries()` until the returned value from the function is falsy.
|
||||||
|
- Return the removed elements, using `Array.prototype.slice()`.
|
||||||
|
- The callback function, `fn`, accepts a single argument which is the value of the element.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const takeRightWhile = (arr, func) =>
|
const takeRightWhile = (arr, fn) => {
|
||||||
arr.reduceRight((acc, el) => (func(el) ? acc : [el, ...acc]), []);
|
for (const [i, val] of [...arr].reverse().entries())
|
||||||
|
if (!fn(val)) return i === 0 ? [] : arr.slice(-i);
|
||||||
|
return arr;
|
||||||
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
takeRightWhile([1, 2, 3, 4], n => n < 3); // [3, 4]
|
takeRightWhile([1, 2, 3, 4], n => n >= 3); // [3, 4]
|
||||||
```
|
```
|
||||||
|
|||||||
22
snippets/takeUntil.md
Normal file
22
snippets/takeUntil.md
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
---
|
||||||
|
title: takeUntil
|
||||||
|
tags: array,intermediate
|
||||||
|
---
|
||||||
|
|
||||||
|
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.prototype.entries()` until the returned value from the function is truthy.
|
||||||
|
- Return the removed elements, using `Array.prototype.slice()`.
|
||||||
|
- The callback function, `fn`, accepts a single argument which is the value of the element.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const takeUntil = (arr, fn) => {
|
||||||
|
for (const [i, val] of arr.entries()) if (fn(val)) return arr.slice(0, i);
|
||||||
|
return arr;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
takeUntil([1, 2, 3, 4], n => n >= 3); // [1, 2]
|
||||||
|
```
|
||||||
@ -6,12 +6,13 @@ tags: array,intermediate
|
|||||||
Removes elements in an array until the passed function returns `false`.
|
Removes elements in an array until the passed function returns `false`.
|
||||||
Returns the removed elements.
|
Returns the removed elements.
|
||||||
|
|
||||||
- Loop through the array, using a `for...of` loop over `Array.prototype.entries()` until the returned value from the function is `false`.
|
- Loop through the array, using a `for...of` loop over `Array.prototype.entries()` until the returned value from the function is falsy.
|
||||||
- Return the removed elements, using `Array.prototype.slice()`.
|
- Return the removed elements, using `Array.prototype.slice()`.
|
||||||
|
- The callback function, `fn`, accepts a single argument which is the value of the element.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const takeWhile = (arr, func) => {
|
const takeWhile = (arr, fn) => {
|
||||||
for (const [i, val] of arr.entries()) if (!func(val)) return arr.slice(0, i);
|
for (const [i, val] of arr.entries()) if (!fn(val)) return arr.slice(0, i);
|
||||||
return arr;
|
return arr;
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user