diff --git a/README.md b/README.md index 332fba4bc..21d6bafb2 100644 --- a/README.md +++ b/README.md @@ -457,18 +457,15 @@ const percentile = (arr, val) => ### Pick -Use `.reduce()` to convert the filtered/picked keys back to a object with the corresponding key:value pair if the key exist in the obj. +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. ```js -const pick = (obj, arr) => arr.reduce((acc, curr) => (curr in obj && (acc[curr] = obj[curr]), acc), {}); - -// const object = { 'a': 1, 'b': '2', 'c': 3 }; -// pick(object, ['a', 'c']) -> { 'a': 1, 'c': 3 } - +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 -// pick(object, ['a', 'c'])['c'] -> 3 - ``` + ### Pipe Use `Array.reduce()` to pass value through functions. diff --git a/snippets/pick.md b/snippets/pick.md index c3b5cd2f2..3689e4a28 100644 --- a/snippets/pick.md +++ b/snippets/pick.md @@ -1,14 +1,10 @@ ### Pick -Use `.reduce()` to convert the filtered/picked keys back to a object with the corresponding key:value pair if the key exist in the obj. +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. ```js -const pick = (obj, arr) => arr.reduce((acc, curr) => (curr in obj && (acc[curr] = obj[curr]), acc), {}); - -// const object = { 'a': 1, 'b': '2', 'c': 3 }; -// pick(object, ['a', 'c']) -> { 'a': 1, 'c': 3 } - +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 -// pick(object, ['a', 'c'])['c'] -> 3 - -``` \ No newline at end of file +```