diff --git a/README.md b/README.md
index adec6adb1..20cc4a0f8 100644
--- a/README.md
+++ b/README.md
@@ -20,7 +20,7 @@
#### Related projects
- [30 Seconds of CSS](https://30-seconds.github.io/30-seconds-of-css/)
-- [30 Seconds of Interviews](https://30secondsofinterviews.org/)
+- [30 Seconds of Interviews](https://30secondsofinterviews.org/)
- [30 Seconds of Python](https://github.com/kriadmin/30-seconds-of-python-code) *(unofficial)*
- [30 Seconds of PHP](https://github.com/appzcoder/30-seconds-of-php-code) *(unofficial)*
@@ -503,7 +503,7 @@ average(1, 2, 3);
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 (`...`).
+Call the provided function, `fn`, with up to `n` arguments, using `Array.prototype.slice(0,n)` and the spread operator (`...`).
```js
const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
@@ -521,12 +521,12 @@ const firstTwoMax = ary(Math.max, 2);
[⬆ Back to top](#table-of-contents)
-### 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.
-
+### 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.
+
```js
const call = (key, ...args) => context => context[key](...args);
```
@@ -542,18 +542,18 @@ const map = call.bind(null, 'map');
Promise.resolve([1, 2, 3])
.then(map(x => 2 * x))
.then(console.log); //[ 2, 4, 6 ]
-```
+```
[⬆ Back to top](#table-of-contents)
-### collectInto
-
-Changes a function that accepts an array into a variadic function.
-
-Given a function, return a closure that collects all inputs into an array-accepting function.
-
+### collectInto
+
+Changes a function that accepts an array into a variadic function.
+
+Given a function, return a closure that collects all inputs into an array-accepting function.
+
```js
const collectInto = fn => (...args) => fn(args);
```
@@ -567,18 +567,18 @@ let p1 = Promise.resolve(1);
let p2 = Promise.resolve(2);
let p3 = new Promise(resolve => setTimeout(resolve, 2000, 3));
Pall(p1, p2, p3).then(console.log); // [1, 2, 3] (after about 2 seconds)
-```
+```
[⬆ Back to top](#table-of-contents)
-### flip
-
-Flip takes a function as an argument, then makes the first argument the last.
-
-Return a closure that takes variadic inputs, and splices the last argument to make it the first argument before applying the rest.
-
+### flip
+
+Flip takes a function as an argument, then makes the first argument the last.
+
+Return a closure that takes variadic inputs, and splices the last argument to make it the first argument before applying the rest.
+
```js
const flip = fn => (first, ...rest) => fn(...rest, first);
```
@@ -594,7 +594,7 @@ let mergePerson = mergeFrom.bind(null, a);
mergePerson(b); // == b
b = {};
Object.assign(b, a); // == b
-```
+```
@@ -604,7 +604,7 @@ Object.assign(b, a); // == b
Creates a function that invokes each provided function with the arguments it receives and returns the results.
-Use `Array.map()` and `Function.apply()` to apply each function to the given arguments.
+Use `Array.prototype.map()` and `Function.prototype.apply()` to apply each function to the given arguments.
```js
const over = (...fns) => (...args) => fns.map(fn => fn.apply(null, args));
@@ -626,7 +626,7 @@ minMax(1, 2, 3, 4, 5); // [1,5]
Creates a function that invokes the provided function with its arguments transformed.
-Use `Array.map()` to apply `transforms` to `args` in combination with the spread operator (`...`) to pass the transformed arguments to `fn`.
+Use `Array.prototype.map()` to apply `transforms` to `args` in combination with the spread operator (`...`) to pass the transformed arguments to `fn`.
```js
const overArgs = (fn, transforms) => (...args) => fn(...args.map((val, i) => transforms[i](val)));
@@ -650,7 +650,7 @@ fn(9, 3); // [81, 6]
Performs left-to-right function composition for asynchronous functions.
-Use `Array.reduce()` with the spread operator (`...`) to perform left-to-right function composition using `Promise.then()`.
+Use `Array.prototype.reduce()` with the spread operator (`...`) to perform left-to-right function composition using `Promise.then()`.
The functions can return a combination of: simple values, `Promise`'s, or they can be defined as `async` ones returning through `await`.
All functions must be unary.
@@ -681,7 +681,7 @@ const sum = pipeAsyncFunctions(
Performs left-to-right function composition.
-Use `Array.reduce()` with the spread operator (`...`) to perform left-to-right function composition.
+Use `Array.prototype.reduce()` with the spread operator (`...`) to perform left-to-right function composition.
The first (leftmost) function can accept one or more arguments; the remaining functions must be unary.
```js
@@ -734,7 +734,7 @@ delay(2000).then(() => console.log('Hi!')); // // Promise resolves after 2s
Creates a function that invokes the provided function with its arguments arranged according to the specified indexes.
-Use `Array.map()` to reorder arguments based on `indexes` in combination with the spread operator (`...`) to pass the transformed arguments to `fn`.
+Use `Array.prototype.map()` to reorder arguments based on `indexes` in combination with the spread operator (`...`) to pass the transformed arguments to `fn`.
```js
const rearg = (fn, indexes) => (...args) => fn(...indexes.map(i => args[i]));
@@ -757,12 +757,12 @@ rearged('b', 'c', 'a'); // ['a', 'b', 'c']
[⬆ Back to top](#table-of-contents)
-### spreadOver
-
-Takes a variadic function and returns a closure that accepts an array of arguments to map to the inputs of the function.
-
-Use closures and the spread operator (`...`) to map the array of arguments to the inputs of the function.
-
+### spreadOver
+
+Takes a variadic function and returns a closure that accepts an array of arguments to map to the inputs of the function.
+
+Use closures and the spread operator (`...`) to map the array of arguments to the inputs of the function.
+
```js
const spreadOver = fn => argsArr => fn(...argsArr);
```
@@ -773,7 +773,7 @@ const spreadOver = fn => argsArr => fn(...argsArr);
```js
const arrayMax = spreadOver(Math.max);
arrayMax([1, 2, 3]); // 3
-```
+```
@@ -809,7 +809,7 @@ const unary = fn => val => fn(val);
Returns `true` if the provided predicate function returns `true` for all elements in a collection, `false` otherwise.
-Use `Array.every()` to test if all elements in the collection return `true` based on `fn`.
+Use `Array.prototype.every()` to test if all elements in the collection return `true` based on `fn`.
Omit the second argument, `fn`, to use `Boolean` as a default.
```js
@@ -832,7 +832,7 @@ all([1, 2, 3]); // true
Check if all elements in an array are equal.
-Use `Array.every()` to check if all the elements of the array are the same as the first one.
+Use `Array.prototype.every()` to check if all the elements of the array are the same as the first one.
```js
const allEqual = arr => arr.every(val => val === arr[0]);
@@ -854,7 +854,7 @@ allEqual([1, 1, 1, 1]); // true
Returns `true` if the provided predicate function returns `true` for at least one element in a collection, `false` otherwise.
-Use `Array.some()` to test if any elements in the collection return `true` based on `fn`.
+Use `Array.prototype.some()` to test if any elements in the collection return `true` based on `fn`.
Omit the second argument, `fn`, to use `Boolean` as a default.
```js
@@ -877,8 +877,8 @@ any([0, 0, 1, 0]); // true
Converts a 2D array to a comma-separated values (CSV) string.
-Use `Array.map()` and `Array.join(delimiter)` to combine individual 1D arrays (rows) into strings.
-Use `Array.join('\n')` to combine all rows into a CSV string, separating each row with a newline.
+Use `Array.prototype.map()` and `Array.prototype.join(delimiter)` to combine individual 1D arrays (rows) into strings.
+Use `Array.prototype.join('\n')` to combine all rows into a CSV string, separating each row with a newline.
Omit the second argument, `delimiter`, to use a default delimiter of `,`.
```js
@@ -902,7 +902,7 @@ arrayToCSV([['a', 'b'], ['c', 'd']], ';'); // '"a";"b"\n"c";"d"'
Splits values into two groups. If an element in `filter` is truthy, the corresponding element in the collection belongs to the first group; otherwise, it belongs to the second group.
-Use `Array.reduce()` and `Array.push()` to add elements to groups, based on `filter`.
+Use `Array.prototype.reduce()` and `Array.prototype.push()` to add elements to groups, based on `filter`.
```js
const bifurcate = (arr, filter) =>
@@ -924,7 +924,7 @@ bifurcate(['beep', 'boop', 'foo', 'bar'], [true, true, false, true]); // [ ['bee
Splits values into two groups according to a predicate function, which specifies which group an element in the input collection belongs to. If the predicate function returns a truthy value, the collection element belongs to the first group; otherwise, it belongs to the second group.
-Use `Array.reduce()` and `Array.push()` to add elements to groups, based on the value returned by `fn` for each element.
+Use `Array.prototype.reduce()` and `Array.prototype.push()` to add elements to groups, based on the value returned by `fn` for each element.
```js
const bifurcateBy = (arr, fn) =>
@@ -947,7 +947,7 @@ bifurcateBy(['beep', 'boop', 'foo', 'bar'], x => x[0] === 'b'); // [ ['beep', 'b
Chunks an array into smaller arrays of a specified size.
Use `Array.from()` to create a new array, that fits the number of chunks that will be produced.
-Use `Array.slice()` to map each element of the new array to a chunk the length of `size`.
+Use `Array.prototype.slice()` to map each element of the new array to a chunk the length of `size`.
If the original array can't be split evenly, the final chunk will contain the remaining elements.
```js
@@ -972,7 +972,7 @@ chunk([1, 2, 3, 4, 5], 2); // [[1,2],[3,4],[5]]
Removes falsey values from an array.
-Use `Array.filter()` to filter out falsey values (`false`, `null`, `0`, `""`, `undefined`, and `NaN`).
+Use `Array.prototype.filter()` to filter out falsey values (`false`, `null`, `0`, `""`, `undefined`, and `NaN`).
```js
const compact = arr => arr.filter(Boolean);
@@ -993,8 +993,8 @@ compact([0, 1, false, 2, '', 3, 'a', 'e' * 23, NaN, 's', 34]); // [ 1, 2, 3, 'a'
Groups the elements of an array based on the given function and returns the count of elements in each group.
-Use `Array.map()` to map the values of an array to a function or property name.
-Use `Array.reduce()` to create an object, where the keys are produced from the mapped results.
+Use `Array.prototype.map()` to map the values of an array to a function or property name.
+Use `Array.prototype.reduce()` to create an object, where the keys are produced from the mapped results.
```js
const countBy = (arr, fn) =>
@@ -1020,7 +1020,7 @@ countBy(['one', 'two', 'three'], 'length'); // {3: 2, 5: 1}
Counts the occurrences of a value in an array.
-Use `Array.reduce()` to increment a counter each time you encounter the specific value inside the array.
+Use `Array.prototype.reduce()` to increment a counter each time you encounter the specific value inside the array.
```js
const countOccurrences = (arr, val) => arr.reduce((a, v) => (v === val ? a + 1 : a), 0);
@@ -1042,7 +1042,7 @@ countOccurrences([1, 1, 2, 1, 2, 3], 1); // 3
Deep flattens an array.
Use recursion.
-Use `Array.concat()` with an empty array (`[]`) and the spread operator (`...`) to flatten an array.
+Use `Array.prototype.concat()` with an empty array (`[]`) and the spread operator (`...`) to flatten an array.
Recursively flatten each element that is an array.
```js
@@ -1064,7 +1064,7 @@ deepFlatten([1, [2], [[3], 4], 5]); // [1,2,3,4,5]
Returns the difference between two arrays.
-Create a `Set` from `b`, then use `Array.filter()` on `a` to only keep values not contained in `b`.
+Create a `Set` from `b`, then use `Array.prototype.filter()` on `a` to only keep values not contained in `b`.
```js
const difference = (a, b) => {
@@ -1088,7 +1088,7 @@ difference([1, 2, 3], [1, 2, 4]); // [3]
Returns the difference between two arrays, after applying the provided function to each array element of both.
-Create a `Set` by applying `fn` to each element in `b`, then use `Array.filter()` in combination with `fn` on `a` to only keep values not contained in the previously created set.
+Create a `Set` by applying `fn` to each element in `b`, then use `Array.prototype.filter()` in combination with `fn` on `a` to only keep values not contained in the previously created set.
```js
const differenceBy = (a, b, fn) => {
@@ -1113,7 +1113,7 @@ differenceBy([{ x: 2 }, { x: 1 }], [{ x: 1 }], v => v.x); // [ { x: 2 } ]
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.
+Use `Array.prototype.filter()` and `Array.prototype.findIndex()` to find the appropriate values.
```js
const differenceWith = (arr, val, comp) => arr.filter(a => val.findIndex(b => comp(a, b)) === -1);
@@ -1134,7 +1134,7 @@ differenceWith([1, 1.2, 1.5, 3, 0], [1.9, 3, 0], (a, b) => Math.round(a) === Mat
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.
+Use `Array.prototype.slice()` to slice the remove the specified number of elements from the left.
```js
const drop = (arr, n = 1) => arr.slice(n);
@@ -1157,7 +1157,7 @@ drop([1, 2, 3], 42); // []
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.
+Use `Array.prototype.slice()` to slice the remove the specified number of elements from the right.
```js
const dropRight = (arr, n = 1) => arr.slice(0, -n);
@@ -1180,7 +1180,7 @@ dropRight([1, 2, 3], 42); // []
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`.
+Loop through the array, using `Array.prototype.slice()` to drop the last element of the array until the returned value from the function is `true`.
Returns the remaining elements.
```js
@@ -1205,7 +1205,7 @@ dropRightWhile([1, 2, 3, 4], n => n < 3); // [1, 2]
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`.
+Loop through the array, using `Array.prototype.slice()` to drop the first element of the array until the returned value from the function is `true`.
Returns the remaining elements.
```js
@@ -1230,7 +1230,7 @@ dropWhile([1, 2, 3, 4], n => n >= 3); // [3,4]
Returns every nth element in an array.
-Use `Array.filter()` to create a new array that contains every nth element of a given array.
+Use `Array.prototype.filter()` to create a new array that contains every nth element of a given array.
```js
const everyNth = (arr, nth) => arr.filter((e, i) => i % nth === nth - 1);
@@ -1251,7 +1251,7 @@ everyNth([1, 2, 3, 4, 5, 6], 2); // [ 2, 4, 6 ]
Filters out the non-unique values in an array.
-Use `Array.filter()` for an array containing only the unique values.
+Use `Array.prototype.filter()` for an array containing only the unique values.
```js
const filterNonUnique = arr => arr.filter(i => arr.indexOf(i) === arr.lastIndexOf(i));
@@ -1272,7 +1272,7 @@ filterNonUnique([1, 2, 2, 3, 4, 4, 5]); // [1, 3, 5]
Filters out the non-unique values in an array, based on a provided comparator function.
-Use `Array.filter()` and `Array.every()` for an array containing only the unique values, based on the comparator function, `fn`.
+Use `Array.prototype.filter()` and `Array.prototype.every()` for an array containing only the unique values, based on the comparator function, `fn`.
The comparator function takes four arguments: the values of the two elements being compared and their indexes.
```js
@@ -1304,7 +1304,7 @@ filterNonUniqueBy(
Returns the last element for which the provided function returns a truthy value.
-Use `Array.filter()` to remove elements for which `fn` returns falsey values, `Array.pop()` to get the last one.
+Use `Array.prototype.filter()` to remove elements for which `fn` returns falsey values, `Array.prototype.pop()` to get the last one.
```js
const findLast = (arr, fn) => arr.filter(fn).pop();
@@ -1325,8 +1325,8 @@ findLast([1, 2, 3, 4], n => n % 2 === 1); // 3
Returns the index of the last element for which the provided function returns a truthy value.
-Use `Array.map()` to map each element to an array with its index and value.
-Use `Array.filter()` to remove elements for which `fn` returns falsey values, `Array.pop()` to get the last one.
+Use `Array.prototype.map()` to map each element to an array with its index and value.
+Use `Array.prototype.filter()` to remove elements for which `fn` returns falsey values, `Array.prototype.pop()` to get the last one.
```js
const findLastIndex = (arr, fn) =>
@@ -1352,7 +1352,7 @@ findLastIndex([1, 2, 3, 4], n => n % 2 === 1); // 2 (index of the value 3)
Flattens an array up to the specified depth.
Use recursion, decrementing `depth` by 1 for each level of depth.
-Use `Array.reduce()` and `Array.concat()` to merge elements or arrays.
+Use `Array.prototype.reduce()` and `Array.prototype.concat()` to merge elements or arrays.
Base case, for `depth` equal to `1` stops recursion.
Omit the second argument, `depth` to flatten only to a depth of `1` (single flatten).
@@ -1377,7 +1377,7 @@ flatten([1, [2, [3, [4, 5], 6], 7], 8], 2); // [1, 2, 3, [4, 5], 6, 7, 8]
Executes a provided function once for each array element, starting from the array's last element.
-Use `Array.slice(0)` to clone the given array, `Array.reverse()` to reverse it and `Array.forEach()` to iterate over the reversed array.
+Use `Array.prototype.slice(0)` to clone the given array, `Array.prototype.reverse()` to reverse it and `Array.prototype.forEach()` to iterate over the reversed array.
```js
const forEachRight = (arr, callback) =>
@@ -1402,8 +1402,8 @@ forEachRight([1, 2, 3, 4], val => console.log(val)); // '4', '3', '2', '1'
Groups the elements of an array based on the given function.
-Use `Array.map()` to map the values of an array to a function or property name.
-Use `Array.reduce()` to create an object, where the keys are produced from the mapped results.
+Use `Array.prototype.map()` to map the values of an array to a function or property name.
+Use `Array.prototype.reduce()` to create an object, where the keys are produced from the mapped results.
```js
const groupBy = (arr, fn) =>
@@ -1451,7 +1451,7 @@ head([1, 2, 3]); // 1
Returns all indices of `val` in an array.
If `val` never occurs, returns `[]`.
-Use `Array.reduce()` to loop over elements and store indices for matching elements.
+Use `Array.prototype.reduce()` to loop over elements and store indices for matching elements.
Return the array of indices.
```js
@@ -1495,7 +1495,7 @@ initial([1, 2, 3]); // [1,2]
Initializes a 2D array of given width and height and value.
-Use `Array.map()` to generate h rows where each is a new array of size w initialize with value. If the value is not provided, default to `null`.
+Use `Array.prototype.map()` to generate h rows where each is a new array of size w initialize with value. If the value is not provided, default to `null`.
```js
const initialize2DArray = (w, h, val = null) =>
@@ -1543,7 +1543,7 @@ initializeArrayWithRange(9, 0, 2); // [0,2,4,6,8]
Initializes an array containing the numbers in the specified range (in reverse) where `start` and `end` are inclusive with their common difference `step`.
-Use `Array.from(Math.ceil((end+1-start)/step))` to create an array of the desired length(the amounts of elements is equal to `(end-start)/step` or `(end+1-start)/step` for inclusive end), `Array.map()` to fill with the desired values in a range.
+Use `Array.from(Math.ceil((end+1-start)/step))` to create an array of the desired length(the amounts of elements is equal to `(end-start)/step` or `(end+1-start)/step` for inclusive end), `Array.prototype.map()` to fill with the desired values in a range.
You can omit `start` to use a default value of `0`.
You can omit `step` to use a default value of `1`.
@@ -1594,7 +1594,7 @@ initializeArrayWithValues(5, 2); // [2, 2, 2, 2, 2]
Create a n-dimensional array with given value.
Use recursion.
-Use `Array.map()` to generate rows where each is a new array initialized using `initializeNDArray`.
+Use `Array.prototype.map()` to generate rows where each is a new array initialized using `initializeNDArray`.
```js
const initializeNDArray = (val, ...args) =>
@@ -1619,7 +1619,7 @@ initializeNDArray(5, 2, 2, 2); // [[[5,5],[5,5]],[[5,5],[5,5]]]
Returns a list of elements that exist in both arrays.
-Create a `Set` from `b`, then use `Array.filter()` on `a` to only keep values contained in `b`.
+Create a `Set` from `b`, then use `Array.prototype.filter()` on `a` to only keep values contained in `b`.
```js
const intersection = (a, b) => {
@@ -1643,7 +1643,7 @@ intersection([1, 2, 3], [4, 3, 2]); // [2, 3]
Returns a list of elements that exist in both arrays, after applying the provided function to each array element of both.
-Create a `Set` by applying `fn` to all elements in `b`, then use `Array.filter()` on `a` to only keep elements, which produce values contained in `b` when `fn` is applied to them.
+Create a `Set` by applying `fn` to all elements in `b`, then use `Array.prototype.filter()` on `a` to only keep elements, which produce values contained in `b` when `fn` is applied to them.
```js
const intersectionBy = (a, b, fn) => {
@@ -1667,7 +1667,7 @@ intersectionBy([2.1, 1.2], [2.3, 3.4], Math.floor); // [2.1]
Returns a list of elements that exist in both arrays, using a provided comparator function.
-Use `Array.filter()` and `Array.findIndex()` in combination with the provided comparator to determine intersecting values.
+Use `Array.prototype.filter()` and `Array.prototype.findIndex()` in combination with the provided comparator to determine intersecting values.
```js
const intersectionWith = (a, b, comp) => a.filter(x => b.findIndex(y => comp(x, y)) !== -1);
@@ -1721,7 +1721,7 @@ isSorted([4, 3, 5]); // 0
Joins all elements of an array into a string and returns this string.
Uses a separator and an end separator.
-Use `Array.reduce()` to combine elements into a string.
+Use `Array.prototype.reduce()` to combine elements into a string.
Omit the second argument, `separator`, to use a default separator of `','`.
Omit the third argument, `end`, to use the same value as `separator` by default.
@@ -1755,9 +1755,9 @@ join(['pen', 'pineapple', 'apple', 'pen']); // "pen,pineapple,apple,pen"
Converts an array of objects to a comma-separated values (CSV) string that contains only the `columns` specified.
-Use `Array.join(demiliter)` to combine all the names in `columns` to create the first row.
-Use `Array.map()` and `Array.reduce()` to create a row for each object, substituting non-existent values with empty strings and only mapping values in `columns`.
-Use `Array.join('\n')` to combine all rows into a string.
+Use `Array.prototype.join(demiliter)` to combine all the names in `columns` to create the first row.
+Use `Array.prototype.map()` and `Array.prototype.reduce()` to create a row for each object, substituting non-existent values with empty strings and only mapping values in `columns`.
+Use `Array.prototype.join('\n')` to combine all rows into a string.
Omit the third argument, `delimiter`, to use a default delimiter of `,`.
```js
@@ -1812,7 +1812,7 @@ Takes any number of iterable objects or objects with a `length` property and ret
If multiple objects have the same length, the first one will be returned.
Returns `undefined` if no arguments are provided.
-Use `Array.reduce()`, comparing the `length` of objects to find the longest one.
+Use `Array.prototype.reduce()`, comparing the `length` of objects to find the longest one.
```js
const longestItem = (val, ...vals) =>
@@ -1864,8 +1864,8 @@ squareIt([1, 2, 3]); // { 1: 1, 2: 4, 3: 9 }
Returns the `n` maximum elements from the provided array.
If `n` is greater than or equal to the provided array's length, then return the original array (sorted in descending order).
-Use `Array.sort()` combined with the spread operator (`...`) to create a shallow clone of the array and sort it in descending order.
-Use `Array.slice()` to get the specified number of elements.
+Use `Array.prototype.sort()` combined with the spread operator (`...`) to create a shallow clone of the array and sort it in descending order.
+Use `Array.prototype.slice()` to get the specified number of elements.
Omit the second argument, `n`, to get a one-element array.
```js
@@ -1889,8 +1889,8 @@ maxN([1, 2, 3], 2); // [3,2]
Returns the `n` minimum elements from the provided array.
If `n` is greater than or equal to the provided array's length, then return the original array (sorted in ascending order).
-Use `Array.sort()` combined with the spread operator (`...`) to create a shallow clone of the array and sort it in ascending order.
-Use `Array.slice()` to get the specified number of elements.
+Use `Array.prototype.sort()` combined with the spread operator (`...`) to create a shallow clone of the array and sort it in ascending order.
+Use `Array.prototype.slice()` to get the specified number of elements.
Omit the second argument, `n`, to get a one-element array.
```js
@@ -1913,7 +1913,7 @@ minN([1, 2, 3], 2); // [1,2]
Returns `true` if the provided predicate function returns `false` for all elements in a collection, `false` otherwise.
-Use `Array.some()` to test if any elements in the collection return `true` based on `fn`.
+Use `Array.prototype.some()` to test if any elements in the collection return `true` based on `fn`.
Omit the second argument, `fn`, to use `Boolean` as a default.
```js
@@ -1936,7 +1936,7 @@ none([0, 0, 0]); // true
Returns the nth element of an array.
-Use `Array.slice()` to get an array containing the nth element at the first place.
+Use `Array.prototype.slice()` to get an array containing the nth element at the first place.
If the index is out of bounds, return `undefined`.
Omit the second argument, `n`, to get the first element of the array.
@@ -1960,7 +1960,7 @@ nthElement(['a', 'b', 'b'], -3); // 'a'
Moves the specified amount of elements to the end of the array.
-Use `Array.slice()` twice to get the elements after the specified index and the elements before that.
+Use `Array.prototype.slice()` twice to get the elements after the specified index and the elements before that.
Use the spread operator(`...`) to combine the two into one array.
If `offset` is negative, the elements will be moved from end to start.
@@ -1984,8 +1984,8 @@ offset([1, 2, 3, 4, 5], -2); // [4, 5, 1, 2, 3]
Groups the elements into two arrays, depending on the provided function's truthiness for each element.
-Use `Array.reduce()` to create an array of two arrays.
-Use `Array.push()` to add elements for which `fn` returns `true` to the first array and elements for which `fn` returns `false` to the second one.
+Use `Array.prototype.reduce()` to create an array of two arrays.
+Use `Array.prototype.push()` to add elements for which `fn` returns `true` to the first array and elements for which `fn` returns `false` to the second one.
```js
const partition = (arr, fn) =>
@@ -2018,7 +2018,7 @@ Generates all permutations of an array's elements (contains duplicates).
Use recursion.
For each element in the given array, create all the partial permutations for the rest of its elements.
-Use `Array.map()` to combine the element with each partial permutation, then `Array.reduce()` to combine all permutations in one array.
+Use `Array.prototype.map()` to combine the element with each partial permutation, then `Array.prototype.reduce()` to combine all permutations in one array.
Base cases are for array `length` equal to `2` or `1`.
```js
@@ -2049,8 +2049,8 @@ permutations([1, 33, 5]); // [ [ 1, 33, 5 ], [ 1, 5, 33 ], [ 33, 1, 5 ], [ 33, 5
Mutates the original array to filter out the values specified.
-Use `Array.filter()` and `Array.includes()` to pull out the values that are not needed.
-Use `Array.length = 0` to mutate the passed in an array by resetting it's length to zero and `Array.push()` to re-populate it with only the pulled values.
+Use `Array.prototype.filter()` and `Array.prototype.includes()` to pull out the values that are not needed.
+Use `Array.prototype.length = 0` to mutate the passed in an array by resetting it's length to zero and `Array.prototype.push()` to re-populate it with only the pulled values.
_(For a snippet that does not mutate the original array see [`without`](#without))_
@@ -2079,9 +2079,9 @@ pull(myArray, 'a', 'c'); // myArray = [ 'b', 'b' ]
Mutates the original array to filter out the values at the specified indexes.
-Use `Array.filter()` and `Array.includes()` to pull out the values that are not needed.
-Use `Array.length = 0` to mutate the passed in an array by resetting it's length to zero and `Array.push()` to re-populate it with only the pulled values.
-Use `Array.push()` to keep track of pulled values
+Use `Array.prototype.filter()` and `Array.prototype.includes()` to pull out the values that are not needed.
+Use `Array.prototype.length = 0` to mutate the passed in an array by resetting it's length to zero and `Array.prototype.push()` to re-populate it with only the pulled values.
+Use `Array.prototype.push()` to keep track of pulled values
```js
const pullAtIndex = (arr, pullArr) => {
@@ -2111,9 +2111,9 @@ let pulled = pullAtIndex(myArray, [1, 3]); // myArray = [ 'a', 'c' ] , pulled =
Mutates the original array to filter out the values specified. Returns the removed elements.
-Use `Array.filter()` and `Array.includes()` to pull out the values that are not needed.
-Use `Array.length = 0` to mutate the passed in an array by resetting it's length to zero and `Array.push()` to re-populate it with only the pulled values.
-Use `Array.push()` to keep track of pulled values
+Use `Array.prototype.filter()` and `Array.prototype.includes()` to pull out the values that are not needed.
+Use `Array.prototype.length = 0` to mutate the passed in an array by resetting it's length to zero and `Array.prototype.push()` to re-populate it with only the pulled values.
+Use `Array.prototype.push()` to keep track of pulled values
```js
const pullAtValue = (arr, pullArr) => {
@@ -2143,9 +2143,9 @@ let pulled = pullAtValue(myArray, ['b', 'd']); // myArray = [ 'a', 'c' ] , pulle
Mutates the original array to filter out the values specified, based on a given iterator function.
Check if the last argument provided in a function.
-Use `Array.map()` to apply the iterator function `fn` to all array elements.
-Use `Array.filter()` and `Array.includes()` to pull out the values that are not needed.
-Use `Array.length = 0` to mutate the passed in an array by resetting it's length to zero and `Array.push()` to re-populate it with only the pulled values.
+Use `Array.prototype.map()` to apply the iterator function `fn` to all array elements.
+Use `Array.prototype.filter()` and `Array.prototype.includes()` to pull out the values that are not needed.
+Use `Array.prototype.length = 0` to mutate the passed in an array by resetting it's length to zero and `Array.prototype.push()` to re-populate it with only the pulled values.
```js
const pullBy = (arr, ...args) => {
@@ -2175,8 +2175,8 @@ pullBy(myArray, [{ x: 1 }, { x: 3 }], o => o.x); // myArray = [{ x: 2 }]
Filter an array of objects based on a condition while also filtering out unspecified keys.
-Use `Array.filter()` to filter the array based on the predicate `fn` so that it returns the objects for which the condition returned a truthy value.
-On the filtered array, use `Array.map()` to return the new object using `Array.reduce()` to filter out the keys which were not supplied as the `keys` argument.
+Use `Array.prototype.filter()` to filter the array based on the predicate `fn` so that it returns the objects for which the condition returned a truthy value.
+On the filtered array, use `Array.prototype.map()` to return the new object using `Array.prototype.reduce()` to filter out the keys which were not supplied as the `keys` argument.
```js
const reducedFilter = (data, keys, fn) =>
@@ -2216,7 +2216,7 @@ reducedFilter(data, ['id', 'name'], item => item.age > 24); // [{ id: 2, name: '
Applies a function against an accumulator and each element in the array (from left to right), returning an array of successively reduced values.
-Use `Array.reduce()` to apply the given function to the given array, storing each new result.
+Use `Array.prototype.reduce()` to apply the given function to the given array, storing each new result.
```js
const reduceSuccessive = (arr, fn, acc) =>
@@ -2238,7 +2238,7 @@ reduceSuccessive([1, 2, 3, 4, 5, 6], (acc, val) => acc + val, 0); // [0, 1, 3, 6
Returns the minimum/maximum value of an array, after applying the provided function to set comparing rule.
-Use `Array.reduce()` in combination with the `comparator` function to get the appropriate element in the array.
+Use `Array.prototype.reduce()` in combination with the `comparator` function to get the appropriate element in the array.
You can omit the second parameter, `comparator`, to use the default one that returns the minimum element in the array.
```js
@@ -2264,7 +2264,7 @@ reduceWhich(
### reject
-Takes a predicate and array, like `Array.filter()`, but only keeps `x` if `pred(x) === false`.
+Takes a predicate and array, like `Array.prototype.filter()`, but only keeps `x` if `pred(x) === false`.
```js
const reject = (pred, array) => array.filter((...args) => !pred(...args));
@@ -2286,7 +2286,7 @@ reject(word => word.length > 4, ['Apple', 'Pear', 'Kiwi', 'Banana']); // ['Pear'
Removes elements from an array for which the given function returns `false`.
-Use `Array.filter()` to find array elements that return truthy values and `Array.reduce()` to remove elements using `Array.splice()`.
+Use `Array.prototype.filter()` to find array elements that return truthy values and `Array.prototype.reduce()` to remove elements using `Array.prototype.splice()`.
The `func` is invoked with three arguments (`value, index, array`).
```js
@@ -2337,7 +2337,7 @@ sample([3, 7, 9, 11]); // 9
Gets `n` random elements at unique keys from `array` up to the size of `array`.
Shuffle the array using the [Fisher-Yates algorithm](https://github.com/30-seconds/30-seconds-of-code#shuffle).
-Use `Array.slice()` to get the first `n` elements.
+Use `Array.prototype.slice()` to get the first `n` elements.
Omit the second argument, `n` to get only one element at random from the array.
```js
@@ -2365,9 +2365,9 @@ sampleSize([1, 2, 3], 4); // [2,3,1]
### shank
-Has the same functionality as [`Array.prototype.splice()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice), but returning a new array instead of mutating the original array.
+Has the same functionality as [`Array.prototype.prototype.splice()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice), but returning a new array instead of mutating the original array.
-Use `Array.slice()` and `Array.concat()` to get a new array with the new contents after removing existing elements and/or adding new elements.
+Use `Array.prototype.slice()` and `Array.prototype.concat()` to get a new array with the new contents after removing existing elements and/or adding new elements.
Omit the second argument, `index`, to start at `0`.
Omit the third argument, `delCount`, to remove `0` elements.
Omit the fourth argument, `elements`, in order to not add any new elements.
@@ -2428,7 +2428,7 @@ shuffle(foo); // [2, 3, 1], foo = [1, 2, 3]
Returns an array of elements that appear in both arrays.
-Use `Array.filter()` to remove values that are not part of `values`, determined using `Array.includes()`.
+Use `Array.prototype.filter()` to remove values that are not part of `values`, determined using `Array.prototype.includes()`.
```js
const similarity = (arr, values) => arr.filter(v => values.includes(v));
@@ -2450,7 +2450,7 @@ similarity([1, 2, 3], [1, 2, 4]); // [1, 2]
Returns the lowest index at which value should be inserted into array in order to maintain its sort order.
Check if the array is sorted in descending order (loosely).
-Use `Array.findIndex()` to find the appropriate index where the element should be inserted.
+Use `Array.prototype.findIndex()` to find the appropriate index where the element should be inserted.
```js
const sortedIndex = (arr, n) => {
@@ -2477,7 +2477,7 @@ sortedIndex([30, 50], 40); // 1
Returns the lowest index at which value should be inserted into array in order to maintain its sort order, based on a provided iterator function.
Check if the array is sorted in descending order (loosely).
-Use `Array.findIndex()` to find the appropriate index where the element should be inserted, based on the iterator function `fn`.
+Use `Array.prototype.findIndex()` to find the appropriate index where the element should be inserted, based on the iterator function `fn`.
```js
const sortedIndexBy = (arr, n, fn) => {
@@ -2504,7 +2504,7 @@ sortedIndexBy([{ x: 4 }, { x: 5 }], { x: 4 }, o => o.x); // 0
Returns the highest index at which value should be inserted into array in order to maintain its sort order.
Check if the array is sorted in descending order (loosely).
-Use `Array.reverse()` and `Array.findIndex()` to find the appropriate last index where the element should be inserted.
+Use `Array.prototype.reverse()` and `Array.prototype.findIndex()` to find the appropriate last index where the element should be inserted.
```js
const sortedLastIndex = (arr, n) => {
@@ -2530,8 +2530,8 @@ sortedLastIndex([10, 20, 30, 30, 40], 30); // 4
Returns the highest index at which value should be inserted into array in order to maintain its sort order, based on a provided iterator function.
Check if the array is sorted in descending order (loosely).
-Use `Array.map()` to apply the iterator function to all elements of the array.
-Use `Array.reverse()` and `Array.findIndex()` to find the appropriate last index where the element should be inserted, based on the provided iterator function.
+Use `Array.prototype.map()` to apply the iterator function to all elements of the array.
+Use `Array.prototype.reverse()` and `Array.prototype.findIndex()` to find the appropriate last index where the element should be inserted, based on the provided iterator function.
```js
const sortedLastIndexBy = (arr, n, fn) => {
@@ -2561,9 +2561,9 @@ sortedLastIndexBy([{ x: 4 }, { x: 5 }], { x: 4 }, o => o.x); // 1
Performs stable sorting of an array, preserving the initial indexes of items when their values are the same.
Does not mutate the original array, but returns a new array instead.
-Use `Array.map()` to pair each element of the input array with its corresponding index.
-Use `Array.sort()` and a `compare` function to sort the list, preserving their initial order if the items compared are equal.
-Use `Array.map()` to convert back to the initial array items.
+Use `Array.prototype.map()` to pair each element of the input array with its corresponding index.
+Use `Array.prototype.sort()` and a `compare` function to sort the list, preserving their initial order if the items compared are equal.
+Use `Array.prototype.map()` to convert back to the initial array items.
```js
const stableSort = (arr, compare) =>
@@ -2589,7 +2589,7 @@ const stable = stableSort(arr, () => 0); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Returns the symmetric difference between two arrays, without filtering out duplicate values.
-Create a `Set` from each array, then use `Array.filter()` on each of them to only keep values not contained in the other.
+Create a `Set` from each array, then use `Array.prototype.filter()` on each of them to only keep values not contained in the other.
```js
const symmetricDifference = (a, b) => {
@@ -2615,7 +2615,7 @@ symmetricDifference([1, 2, 2], [1, 3, 1]); // [2, 2, 3]
Returns the symmetric difference between two arrays, after applying the provided function to each array element of both.
-Create a `Set` by applying `fn` to each array's elements, then use `Array.filter()` on each of them to only keep values not contained in the other.
+Create a `Set` by applying `fn` to each array's elements, then use `Array.prototype.filter()` on each of them to only keep values not contained in the other.
```js
const symmetricDifferenceBy = (a, b, fn) => {
@@ -2640,7 +2640,7 @@ symmetricDifferenceBy([2.1, 1.2], [2.3, 3.4], Math.floor); // [ 1.2, 3.4 ]
Returns the symmetric difference between two arrays, using a provided function as a comparator.
-Use `Array.filter()` and `Array.findIndex()` to find the appropriate values.
+Use `Array.prototype.filter()` and `Array.prototype.findIndex()` to find the appropriate values.
```js
const symmetricDifferenceWith = (arr, val, comp) => [
@@ -2668,7 +2668,7 @@ symmetricDifferenceWith(
Returns all elements in an array except for the first one.
-Return `Array.slice(1)` if the array's `length` is more than `1`, otherwise, return the whole array.
+Return `Array.prototype.slice(1)` if the array's `length` is more than `1`, otherwise, return the whole array.
```js
const tail = arr => (arr.length > 1 ? arr.slice(1) : arr);
@@ -2690,7 +2690,7 @@ tail([1]); // [1]
Returns an array with n elements removed from the beginning.
-Use `Array.slice()` to create a slice of the array with `n` elements taken from the beginning.
+Use `Array.prototype.slice()` to create a slice of the array with `n` elements taken from the beginning.
```js
const take = (arr, n = 1) => arr.slice(0, n);
@@ -2712,7 +2712,7 @@ take([1, 2, 3], 0); // []
Returns an array with n elements removed from the end.
-Use `Array.slice()` to create a slice of the array with `n` elements taken from the end.
+Use `Array.prototype.slice()` to create a slice of the array with `n` elements taken from the end.
```js
const takeRight = (arr, n = 1) => arr.slice(arr.length - n, arr.length);
@@ -2734,8 +2734,8 @@ takeRight([1, 2, 3]); // [3]
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()`.
+Loop through the array, using a `for...of` loop over `Array.prototype.keys()` until the returned value from the function is `true`.
+Return the removed elements, using `Array.prototype.reverse()` and `Array.prototype.slice()`.
```js
const takeRightWhile = (arr, func) => {
@@ -2760,8 +2760,8 @@ takeRightWhile([1, 2, 3, 4], n => n < 3); // [3, 4]
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.entries()` until the returned value from the function is `true`.
-Return the removed elements, using `Array.slice()`.
+Loop through the array, using a `for...of` loop over `Array.prototype.entries()` until the returned value from the function is `true`.
+Return the removed elements, using `Array.prototype.slice()`.
```js
const takeWhile = (arr, func) => {
@@ -2785,7 +2785,7 @@ takeWhile([1, 2, 3, 4], n => n >= 3); // [1, 2]
Reduces a given Array-like into a value hash (keyed data store).
-Given an Iterable or Array-like structure, call `Array.prototype.reduce.call()` on the provided object to step over it and return an Object, keyed by the reference value.
+Given an Iterable or Array-like structure, call `Array.prototype.prototype.reduce.call()` on the provided object to step over it and return an Object, keyed by the reference value.
```js
const toHash = (object, key) =>
@@ -2870,7 +2870,7 @@ unionBy([2.1], [1.2, 2.3], Math.floor); // [2.1, 1.2]
Returns every element that exists in any of the two arrays once, using a provided comparator function.
-Create a `Set` with all values of `a` and values in `b` for which the comparator finds no matches in `a`, using `Array.findIndex()`.
+Create a `Set` with all values of `a` and values in `b` for which the comparator finds no matches in `a`, using `Array.prototype.findIndex()`.
```js
const unionWith = (a, b, comp) =>
@@ -2913,7 +2913,7 @@ uniqueElements([1, 2, 2, 3, 4, 4, 5]); // [1, 2, 3, 4, 5]
Returns all unique values of an array, based on a provided comparator function.
-Use `Array.reduce()` and `Array.some()` for an array containing only the first unique occurence of each value, based on the comparator function, `fn`.
+Use `Array.prototype.reduce()` and `Array.prototype.some()` for an array containing only the first unique occurence of each value, based on the comparator function, `fn`.
The comparator function takes two arguments: the values of the two elements being compared.
```js
@@ -2948,7 +2948,7 @@ uniqueElementsBy(
Returns all unique values of an array, based on a provided comparator function.
-Use `Array.reduce()` and `Array.some()` for an array containing only the last unique occurence of each value, based on the comparator function, `fn`.
+Use `Array.prototype.reduce()` and `Array.prototype.some()` for an array containing only the last unique occurence of each value, based on the comparator function, `fn`.
The comparator function takes two arguments: the values of the two elements being compared.
```js
@@ -2983,7 +2983,7 @@ uniqueElementsByRight(
Returns the unique symmetric difference between two arrays, not containing duplicate values from either array.
-Use `Array.filter()` and `Array.includes()` on each array to remove values contained in the other, then create a `Set` from the results, removing duplicate values.
+Use `Array.prototype.filter()` and `Array.prototype.includes()` on each array to remove values contained in the other, then create a `Set` from the results, removing duplicate values.
```js
const uniqueSymmetricDifference = (a, b) => [
@@ -3007,8 +3007,8 @@ uniqueSymmetricDifference([1, 2, 2], [1, 3, 1]); // [2, 3]
Creates an array of arrays, ungrouping the elements in an array produced by [zip](#zip).
-Use `Math.max.apply()` to get the longest subarray in the array, `Array.map()` to make each element an array.
-Use `Array.reduce()` and `Array.forEach()` to map grouped values to individual arrays.
+Use `Math.max.apply()` to get the longest subarray in the array, `Array.prototype.map()` to make each element an array.
+Use `Array.prototype.reduce()` and `Array.prototype.forEach()` to map grouped values to individual arrays.
```js
const unzip = arr =>
@@ -3036,9 +3036,9 @@ unzip([['a', 1, true], ['b', 2]]); //[['a', 'b'], [1, 2], [true]]
Creates an array of elements, ungrouping the elements in an array produced by [zip](#zip) and applying the provided function.
-Use `Math.max.apply()` to get the longest subarray in the array, `Array.map()` to make each element an array.
-Use `Array.reduce()` and `Array.forEach()` to map grouped values to individual arrays.
-Use `Array.map()` and the spread operator (`...`) to apply `fn` to each individual group of elements.
+Use `Math.max.apply()` to get the longest subarray in the array, `Array.prototype.map()` to make each element an array.
+Use `Array.prototype.reduce()` and `Array.prototype.forEach()` to map grouped values to individual arrays.
+Use `Array.prototype.map()` and the spread operator (`...`) to apply `fn` to each individual group of elements.
```js
const unzipWith = (arr, fn) =>
@@ -3067,7 +3067,7 @@ unzipWith([[1, 10, 100], [2, 20, 200]], (...args) => args.reduce((acc, v) => acc
Filters out the elements of an array, that have one of the specified values.
-Use `Array.filter()` to create an array excluding(using `!Array.includes()`) all given values.
+Use `Array.prototype.filter()` to create an array excluding(using `!Array.includes()`) all given values.
_(For a snippet that mutates the original array see [`pull`](#pull))_
@@ -3090,7 +3090,7 @@ without([2, 1, 2, 3], 1, 2); // [3]
Creates a new array out of the two supplied by creating each possible pair from the arrays.
-Use `Array.reduce()`, `Array.map()` and `Array.concat()` to produce every possible pair from the elements of the two arrays and save them in an array.
+Use `Array.prototype.reduce()`, `Array.prototype.map()` and `Array.prototype.concat()` to produce every possible pair from the elements of the two arrays and save them in an array.
```js
const xProd = (a, b) => a.reduce((acc, x) => acc.concat(b.map(y => [x, y])), []);
@@ -3140,7 +3140,7 @@ zip(['a'], [1, 2], [true, false]); // [['a', 1, true], [undefined, 2, false]]
Given an array of valid property identifiers and an array of values, return an object associating the properties to the values.
-Since an object can have undefined values but not undefined property pointers, the array of properties is used to decide the structure of the resulting object using `Array.reduce()`.
+Since an object can have undefined values but not undefined property pointers, the array of properties is used to decide the structure of the resulting object using `Array.prototype.reduce()`.
```js
const zipObject = (props, values) =>
@@ -3205,7 +3205,7 @@ zipWith(
Converts the given array elements into `
` tags and appends them to the list of the given id.
-Use `Array.map()`, `document.querySelector()`, and an anonymous inner closure to create a list of html tags.
+Use `Array.prototype.map()`, `document.querySelector()`, and an anonymous inner closure to create a list of html tags.
```js
const arrayToHtmlList = (arr, listID) =>
@@ -3253,7 +3253,7 @@ bottomVisible(); // true
⚠️ **NOTICE:** The same functionality can be easily implemented by using the new asynchronous Clipboard API, which is still experimental but should be used in the future instead of this snippet. Find out more about it [here](https://github.com/w3c/clipboard-apis/blob/master/explainer.adoc#writing-to-the-clipboard).
-Copy a string to the clipboard.
+Copy a string to the clipboard.
Only works as a result of user action (i.e. inside a `click` event listener).
Create a new `