diff --git a/README.md b/README.md index 433da221a..9ab2a84ed 100644 --- a/README.md +++ b/README.md @@ -102,8 +102,10 @@ average(1, 2, 3); * [`difference`](#difference) * [`differenceBy`](#differenceby) * [`differenceWith`](#differencewith) -* [`dropWhile`](#dropWhile) +* [`drop`](#drop) * [`dropRight`](#dropright) +* [`dropRightWhile`](#droprightwhile) +* [`dropWhile`](#dropwhile) * [`everyNth`](#everynth) * [`filterNonUnique`](#filternonunique) * [`findLast`](#findlast) @@ -842,25 +844,23 @@ differenceWith([1, 1.2, 1.5, 3, 0], [1.9, 3, 0], (a, b) => Math.round(a) === Mat
[⬆ Back to top](#table-of-contents) -### dropWhile +### drop -Removes elements in an array until the passed function returns `true`. Returns the remaining elements in the array. +Returns a new array with `n` elements removed from the left. -Loop through the array, using `Array.slice()` to drop the first element of the array until the returned value from the function is `true`. -Returns the remaining elements. +Use `Array.slice()` to slice the remove the specified number of elements from the left. ```js -const dropWhile = (arr, func) => { - while (arr.length > 0 && !func(arr[0])) arr = arr.slice(1); - return arr; -}; +const drop = (arr, n = 1) => arr.slice(n); ```
Examples ```js -dropWhile([1, 2, 3, 4], n => n >= 3); // [3,4] +drop([1, 2, 3]); // [2,3] +drop([1, 2, 3], 2); // [3] +drop([1, 2, 3], 42); // [] ```
@@ -892,6 +892,58 @@ dropRight([1, 2, 3], 42); // []
[⬆ Back to top](#table-of-contents) +### dropRightWhile + +Removes elements from the end of an array until the passed function returns `true`. Returns the remaining elements in the array. + +Loop through the array, using `Array.slice()` to drop the last element of the array until the returned value from the function is `true`. +Returns the remaining elements. + +```js +const dropRightWhile = (arr, func) => { + while (arr.length > 0 && !func(arr[arr.length - 1])) arr = arr.slice(0, -1); + return arr; +}; +``` + +
+Examples + +```js +dropRightWhile([1, 2, 3, 4], n => n < 3); // [1, 2] +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + +### dropWhile + +Removes elements in an array until the passed function returns `true`. Returns the remaining elements in the array. + +Loop through the array, using `Array.slice()` to drop the first element of the array until the returned value from the function is `true`. +Returns the remaining elements. + +```js +const dropWhile = (arr, func) => { + while (arr.length > 0 && !func(arr[0])) arr = arr.slice(1); + return arr; +}; +``` + +
+Examples + +```js +dropWhile([1, 2, 3, 4], n => n >= 3); // [3,4] +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### everyNth Returns every nth element in an array. diff --git a/docs/index.html b/docs/index.html index 76cd8c9aa..aded79766 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,7 @@ scrollToTop(); } }, false); - }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
+      }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
 
const firstTwoMax = ary(Math.max, 2);
 [[2, 6, 'a'], [8, 4, 6], [10]].map(x => firstTwoMax(...x)); // [6, 8, 10]
 

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
@@ -125,15 +125,24 @@ Object.assig
 differenceBy([{ x: 2 }, { x: 1 }], [{ x: 1 }], v => v.x); // [ { x: 2 } ]
 

differenceWith

Filters out all values from an array for which the comparator function does not return true.

Use Array.filter() and Array.findIndex() to find the appropriate values.

const differenceWith = (arr, val, comp) => arr.filter(a => val.findIndex(b => comp(a, b)) === -1);
 
differenceWith([1, 1.2, 1.5, 3, 0], [1.9, 3, 0], (a, b) => Math.round(a) === Math.round(b)); // [1, 1.2]
-

dropWhile

Removes elements in an array until the passed function returns true. Returns the remaining elements in the array.

Loop through the array, using Array.slice() to drop the first element of the array until the returned value from the function is true. Returns the remaining elements.

const dropWhile = (arr, func) => {
-  while (arr.length > 0 && !func(arr[0])) arr = arr.slice(1);
-  return arr;
-};
-
dropWhile([1, 2, 3, 4], n => n >= 3); // [3,4]
+

drop

Returns a new array with n elements removed from the left.

Use Array.slice() to slice the remove the specified number of elements from the left.

const drop = (arr, n = 1) => arr.slice(n);
+
drop([1, 2, 3]); // [2,3]
+drop([1, 2, 3], 2); // [3]
+drop([1, 2, 3], 42); // []
 

dropRight

Returns a new array with n elements removed from the right.

Use Array.slice() to slice the remove the specified number of elements from the right.

const dropRight = (arr, n = 1) => arr.slice(0, -n);
 
dropRight([1, 2, 3]); // [1,2]
 dropRight([1, 2, 3], 2); // [1]
 dropRight([1, 2, 3], 42); // []
+

dropRightWhile

Removes elements from the end of an array until the passed function returns true. Returns the remaining elements in the array.

Loop through the array, using Array.slice() to drop the last element of the array until the returned value from the function is true. Returns the remaining elements.

const dropRightWhile = (arr, func) => {
+  while (arr.length > 0 && !func(arr[arr.length - 1])) arr = arr.slice(0, -1);
+  return arr;
+};
+
dropRightWhile([1, 2, 3, 4], n => n < 3); // [1, 2]
+

dropWhile

Removes elements in an array until the passed function returns true. Returns the remaining elements in the array.

Loop through the array, using Array.slice() to drop the first element of the array until the returned value from the function is true. Returns the remaining elements.

const dropWhile = (arr, func) => {
+  while (arr.length > 0 && !func(arr[0])) arr = arr.slice(1);
+  return arr;
+};
+
dropWhile([1, 2, 3, 4], n => n >= 3); // [3,4]
 

everyNth

Returns every nth element in an array.

Use Array.filter() to create a new array that contains every nth element of a given array.

const everyNth = (arr, nth) => arr.filter((e, i) => i % nth === nth - 1);
 
everyNth([1, 2, 3, 4, 5, 6], 2); // [ 2, 4, 6 ]
 

filterNonUnique

Filters out the non-unique values in an array.

Use Array.filter() for an array containing only the unique values.

const filterNonUnique = arr => arr.filter(i => arr.indexOf(i) === arr.lastIndexOf(i));
diff --git a/snippets/dropRightWhile.md b/snippets/dropRightWhile.md
index 2329e65c0..4077afd24 100644
--- a/snippets/dropRightWhile.md
+++ b/snippets/dropRightWhile.md
@@ -7,7 +7,7 @@ Returns the remaining elements.
 
 ```js
 const dropRightWhile = (arr, func) => {
-  while (arr.length > 0 && !func(arr[arr.length-1])) arr = arr.slice(0, -1);
+  while (arr.length > 0 && !func(arr[arr.length - 1])) arr = arr.slice(0, -1);
   return arr;
 };
 ```