752 B
752 B
title, tags, firstSeen, lastUpdated
| title | tags | firstSeen | lastUpdated |
|---|---|---|---|
| pickBy | object,intermediate | 2018-01-19T13:23:45+02:00 | 2020-10-22T20:24:04+03:00 |
Creates an object composed of the properties the given function returns truthy for.
- Use
Object.keys()andArray.prototype.filter()to remove the keys for whichfnreturns a falsy value. - Use
Array.prototype.reduce()to convert the filtered keys back to an object with the corresponding key-value pairs. - The callback function is invoked with two arguments: (value, key).
const pickBy = (obj, fn) =>
Object.keys(obj)
.filter(k => fn(obj[k], k))
.reduce((acc, key) => ((acc[key] = obj[key]), acc), {});
pickBy({ a: 1, b: '2', c: 3 }, x => typeof x === 'number');
// { 'a': 1, 'c': 3 }