chunk([1, 2, 3, 4, 5], 2); // [[1,2],[3,4],[5]] -
Removes falsey values from an array.
Use Array.prototype.filter() to filter out falsey values (false, null, 0, "", undefined, and NaN).
const compact = arr => arr.filter(Boolean); +
Removes falsy values from an array.
Use Array.prototype.filter() to filter out falsy values (false, null, 0, "", undefined, and NaN).
const compact = arr => arr.filter(Boolean);
compact([0, 1, false, 2, '', 3, 'a', 'e' * 23, NaN, 's', 34]); // [ 1, 2, 3, 'a', 's', 34 ]
Groups the elements of an array based on the given function and returns the count of elements in each group.
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.
const countBy = (arr, fn) => arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val) => { @@ -183,9 +183,9 @@ ], (a, b) => a.id == b.id ); // [ { id: 2, value: 'c' } ] -
Returns the last element for which the provided function returns a truthy value.
Use Array.prototype.filter() to remove elements for which fn returns falsey values, Array.prototype.pop() to get the last one.
const findLast = (arr, fn) => arr.filter(fn).pop(); +
Returns the last element for which the provided function returns a truthy value.
Use Array.prototype.filter() to remove elements for which fn returns falsy values, Array.prototype.pop() to get the last one.
const findLast = (arr, fn) => arr.filter(fn).pop();
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.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.
const findLastIndex = (arr, fn) => +
Returns the index of the last element for which the provided function returns a truthy value.
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 falsy values, Array.prototype.pop() to get the last one.
const findLastIndex = (arr, fn) => arr .map((val, i) => [i, val]) .filter(([i, val]) => fn(val, i, arr)) diff --git a/docs/object.html b/docs/object.html index 243a0d0c1..a74236aa9 100644 --- a/docs/object.html +++ b/docs/object.html @@ -361,7 +361,7 @@ Foo.prototypefilter(k => !arr.includes(k)) .reduce((acc, key) => ((acc[key] = obj[key]), acc), {});
omit({ a: 1, b: '2', c: 3 }, ['b']); // { 'a': 1, 'c': 3 } -
Creates an object composed of the properties the given function returns falsey for. The function is invoked with two arguments: (value, key).
Use Object.keys(obj) and Array.prototype.filter()to remove the keys for which fn returns a truthy value. Use Array.prototype.reduce() to convert the filtered keys back to an object with the corresponding key-value pairs.
const omitBy = (obj, fn) => +
Creates an object composed of the properties the given function returns falsy for. The function is invoked with two arguments: (value, key).
Use Object.keys(obj) and Array.prototype.filter()to remove the keys for which fn returns a truthy value. Use Array.prototype.reduce() to convert the filtered keys back to an object with the corresponding key-value pairs.
const omitBy = (obj, fn) => Object.keys(obj) .filter(k => !fn(obj[k], k)) .reduce((acc, key) => ((acc[key] = obj[key]), acc), {}); @@ -382,7 +382,7 @@ Foo.prototypepick
Picks the key-value pairs corresponding to the given keys from an object.
Use
Array.prototype.reduce()to convert the filtered/picked keys back to an object with the corresponding key-value pairs if the key exists in the object.const pick = (obj, arr) => arr.reduce((acc, curr) => (curr in obj && (acc[curr] = obj[curr]), acc), {});pick({ a: 1, b: '2', c: 3 }, ['a', 'c']); // { 'a': 1, 'c': 3 } -
Creates an object composed of the properties the given function returns truthy for. The function is invoked with two arguments: (value, key).
Use Object.keys(obj) and Array.prototype.filter()to remove the keys for which fn returns a falsey value. Use Array.prototype.reduce() to convert the filtered keys back to an object with the corresponding key-value pairs.
const pickBy = (obj, fn) => +
Creates an object composed of the properties the given function returns truthy for. The function is invoked with two arguments: (value, key).
Use Object.keys(obj) and Array.prototype.filter()to remove the keys for which fn returns a falsy value. Use Array.prototype.reduce() to convert the filtered keys back to an object with the corresponding key-value pairs.
const pickBy = (obj, fn) => Object.keys(obj) .filter(k => fn(obj[k], k)) .reduce((acc, key) => ((acc[key] = obj[key]), acc), {}); 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