Removes elements from an array for which the given function returns false.
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).
const remove = (arr, func) => Array.isArray(arr) ? arr.filter(func).reduce((acc, val) => { - arr.splice(arr.indexOf(val), 1); - return acc.concat(val); - }, []) + arr.splice(arr.indexOf(val), 1); + return acc.concat(val); + }, []) : [];
remove([1, 2, 3, 4], n => n % 2 === 0); // [2, 4]
Returns a random element from an array.
Use Math.random() to generate a random number, multiply it by length and round it off to the nearest whole number using Math.floor(). This method also works with strings.
const sample = arr => arr[Math.floor(Math.random() * arr.length)]; diff --git a/docs/type.html b/docs/type.html index e38611ea9..840d0610e 100644 --- a/docs/type.html +++ b/docs/type.html @@ -117,10 +117,8 @@
Checks if the given argument is a native boolean element.
Use typeof to check if a value is classified as a boolean primitive.
const isBoolean = val => typeof val === 'boolean';
isBoolean(null); // false isBoolean(false); // true -
Returns true if the a value is an empty object, collection, map or set, has no enumerable properties or is any type that is not considered a collection.
Check if the provided value is null or if its length is equal to 0.
const isEmpty = val => val == null || !(Object.keys(val) || val).length; -
isEmpty(new Map()); // true -isEmpty(new Set()); // true -isEmpty([]); // true +
Returns true if the a value is an empty object, collection, has no enumerable properties or is any type that is not considered a collection.
Check if the provided value is null or if its length is equal to 0.
const isEmpty = val => val == null || !(Object.keys(val) || val).length; +
isEmpty([]); // true isEmpty({}); // true isEmpty(''); // true isEmpty([1, 2]); // false diff --git a/snippets/checkProp.md b/snippets/checkProp.md index 4644eb666..ef740e3e8 100644 --- a/snippets/checkProp.md +++ b/snippets/checkProp.md @@ -15,6 +15,7 @@ const checkProp = (predicate, prop) => obj => !!predicate(obj[prop]); + const lengthIs4 = checkProp(l => l === 4, 'length'); lengthIs4([]); // false lengthIs4([1,2,3,4]); // true diff --git a/snippets/remove.md b/snippets/remove.md index 58de9c2e0..a8c472774 100644 --- a/snippets/remove.md +++ b/snippets/remove.md @@ -9,9 +9,9 @@ The `func` is invoked with three arguments (`value, index, array`). const remove = (arr, func) => Array.isArray(arr) ? arr.filter(func).reduce((acc, val) => { - arr.splice(arr.indexOf(val), 1); - return acc.concat(val); - }, []) + arr.splice(arr.indexOf(val), 1); + return acc.concat(val); + }, []) : []; ``` diff --git a/test/_30s.js b/test/_30s.js index 194dd4829..c9873d4dc 100644 --- a/test/_30s.js +++ b/test/_30s.js @@ -1001,9 +1001,9 @@ const reject = (pred, array) => array.filter((...args) => !pred(...args)); const remove = (arr, func) => Array.isArray(arr) ? arr.filter(func).reduce((acc, val) => { - arr.splice(arr.indexOf(val), 1); - return acc.concat(val); - }, []) + arr.splice(arr.indexOf(val), 1); + return acc.concat(val); + }, []) : []; const removeNonASCII = str => str.replace(/[^\x20-\x7E]/g, ''); const renameKeys = (keysMap, obj) =>