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) * [`tail`](#tail)
* [`take`](#take) * [`take`](#take)
* [`takeRight`](#takeright) * [`takeRight`](#takeright)
* [`takeRightWhile`](#takerightwhile)
* [`takeWhile`](#takewhile)
* [`union`](#union) * [`union`](#union)
* [`unionBy`](#unionby) * [`unionBy`](#unionby)
* [`unionWith`](#unionwith) * [`unionWith`](#unionwith)
@ -2144,6 +2146,59 @@ takeRight([1, 2, 3]); // [3]
<br>[⬆ Back to top](#table-of-contents) <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 ### union
Returns every element that exists in any of the two arrays once. 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 ```js
const takeWhile = (arr, func) => { const takeWhile = (arr, func) => {
for(let i of arr.keys()) for (let i of arr.keys()) if (func(arr[i])) return arr.slice(0, i);
if (func(arr[i])) return arr.slice(0,i);
return arr; return arr;
}; };
``` ```