Skip to content

Commit

Permalink
Merge pull request #394 from yoshikazusawa/fix-issue-393
Browse files Browse the repository at this point in the history
Fix #393 remove a destructuring assignment example on _.pick native implementation
  • Loading branch information
ODudek authored Apr 2, 2024
2 parents f1abfc5 + 4c82dc8 commit 412f0c1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
11 changes: 2 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2818,17 +2818,10 @@ Creates an object composed of the object properties predicate returns truthy for
var object = { 'a': 1, 'b': '2', 'c': 3 };

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

// Native
const { a, c } = object;
const result = { a, c};
console.log(result);
// output: {a: 1, c: 3}
// for an array of this object --> array.map(({a, c}) => ({a, c}));

// Native
function pick(object, keys) {
return keys.reduce((obj, key) => {
Expand All @@ -2838,7 +2831,7 @@ Creates an object composed of the object properties predicate returns truthy for
return obj;
}, {});
}
var result = pick(object, ['a', 'c']);
var result = pick(object, ['a', 'c', 'x']);
console.log(result)
// output: {a: 1, c: 3}
```
Expand Down
17 changes: 8 additions & 9 deletions tests/unit/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,17 @@ describe('code snippet example', () => {

it('pick', () => {
var object = { 'a': 1, 'b': '2', 'c': 3 };
function pick(object, paths) {
const obj = {};
for (const path of paths) {
if (object[path]) {
obj[path] = object[path]
function pick(object, keys) {
return keys.reduce((obj, key) => {
if (object && object.hasOwnProperty(key)) {
obj[key] = object[key];
}
}
return obj;
return obj;
}, {});
}
assert.deepEqual(
_.pick(object, ['a', 'c']),
pick(object, ['a', 'c'])
_.pick(object, ['a', 'c', 'x']),
pick(object, ['a', 'c', 'x'])
)
})

Expand Down

0 comments on commit 412f0c1

Please sign in to comment.