Skip to content

Latest commit

 

History

History
10 lines (8 loc) · 379 Bytes

pick.md

File metadata and controls

10 lines (8 loc) · 379 Bytes

Pick

Use Array.reduce() to convert the filtered/picked keys back to a object with the corresponding key:value pair if the key exist in the obj.

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 }
// pick(object, ['a', 'c'])['a'] -> 1