Merge remote-tracking branch 'origin/master'

This commit is contained in:
Angelos Chalaris
2018-01-26 13:39:14 +02:00
4 changed files with 69 additions and 4 deletions

View File

@ -151,6 +151,8 @@ average(1, 2, 3);
* [`tail`](#tail)
* [`take`](#take)
* [`takeRight`](#takeright)
* [`takeRightWhile`](#takerightwhile)
* [`takeWhile`](#takewhile)
* [`union`](#union)
* [`unionBy`](#unionby)
* [`unionWith`](#unionwith)
@ -2144,6 +2146,59 @@ takeRight([1, 2, 3]); // [3]
<br>[⬆ Back to top](#table-of-contents)
### 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;
};
```
<details>
<summary>Examples</summary>
```js
takeRightWhile([1, 2, 3, 4], n => n < 3); // [3, 4]
```
</details>
<br>[⬆ Back to top](#table-of-contents)
### 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;
};
```
<details>
<summary>Examples</summary>
```js
takeWhile([1, 2, 3, 4], n => n >= 3); // [1, 2]
```
</details>
<br>[⬆ Back to top](#table-of-contents)
### union
Returns every element that exists in any of the two arrays once.

File diff suppressed because one or more lines are too long

View File

@ -7,8 +7,7 @@ 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 (let i of arr.keys()) if (func(arr[i])) return arr.slice(0, i);
return arr;
};
```