30秒学会 JavaScript 片段 · 2018年10月12日

30秒学会 JavaScript 片段 – pickBy

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), {});

使用样例

pickBy({ a: 1, b: '2', c: 3 }, x => typeof x === 'number'); // { 'a': 1, 'c': 3 }