Skip to content

Commit

Permalink
Merge pull request #163 from Simek/patch-2
Browse files Browse the repository at this point in the history
Utilize reduce in Native _.pick implementation
  • Loading branch information
stevemao committed Dec 28, 2018
2 parents 265769d + 5a7cbe1 commit 2f00af2
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1727,23 +1727,22 @@ Returns a copy of the object, filtered to omit the keys specified.
Creates an object composed of the object properties predicate returns truthy for.

```js
var object = { 'a': 1, 'b': '2', 'c': 3 };
var object = { 'a': 1, 'b': '2', 'c': 3 };

// Underscore/Lodash
var result = _.pick(object, ['a', 'c']);
console.log(result)
// output: {a: 1, c: 3}

// Native
function pick(object, paths) {
const obj = {};
for (const path of paths) {
if (object[path]) {
obj[path] = object[path]
}
}
return obj;
}
function pick(object, keys) {
return keys.reduce((obj, key) => {
if (object[key]) {
obj[key] = object[key];
}
return obj;
}, {});
}
var result = pick(object, ['a', 'c']);
console.log(result)
// output: {a: 1, c: 3}
Expand Down

0 comments on commit 2f00af2

Please sign in to comment.