Update takeWhile snippets

Add takeUntil snippets
This commit is contained in:
Angelos Chalaris
2020-11-29 12:04:53 +02:00
parent 88380b54ad
commit 959c114b0e
4 changed files with 61 additions and 8 deletions

View 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]
```

View File

@ -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
View 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]
```

View File

@ -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;
}; };
``` ```