From b2d737bec5cb1f0e9242d0fdcb87d3ed7c0a2d67 Mon Sep 17 00:00:00 2001 From: King Date: Wed, 13 Dec 2017 16:51:34 -0500 Subject: [PATCH 1/3] add pick code snippiet --- snippets/pick.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 snippets/pick.md diff --git a/snippets/pick.md b/snippets/pick.md new file mode 100644 index 000000000..5523e1fe5 --- /dev/null +++ b/snippets/pick.md @@ -0,0 +1,23 @@ +### Pick + +Use `Objexts.keys()` to convert given object to an iterable arr of keys. +Use `.filter()` to filter the given arr of keys to the expected arr of picked keys. +Use `.reduce()` to convert the filtered/picked keys back to a object with the corresponding key:value pair. + +```js +const pick = (obj, arr) => + Object + .keys(obj) + .filter((v, i) => arr.indexOf(v) !== -1 ) + .reduce((acc, cur, i) => { + acc[cur] = obj[cur]; + return acc; + }, {}); + +// const object = { 'a': 1, 'b': '2', 'c': 3 }; +// pick(object, ['a', 'c']) -> { 'a': 1, 'c': 3 } + +// pick(object, ['a', 'c'])['a'] -> 1 +// pick(object, ['a', 'c'])['c'] -> 3 + +``` \ No newline at end of file From 13c4782c69accfdfe7b8b7f597f6c87f4770e4c8 Mon Sep 17 00:00:00 2001 From: King Date: Wed, 13 Dec 2017 17:41:24 -0500 Subject: [PATCH 2/3] update pick.md -> oneline & simpler solution --- snippets/pick.md | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/snippets/pick.md b/snippets/pick.md index 5523e1fe5..c3b5cd2f2 100644 --- a/snippets/pick.md +++ b/snippets/pick.md @@ -1,18 +1,9 @@ ### Pick -Use `Objexts.keys()` to convert given object to an iterable arr of keys. -Use `.filter()` to filter the given arr of keys to the expected arr of picked keys. -Use `.reduce()` to convert the filtered/picked keys back to a object with the corresponding key:value pair. +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. ```js -const pick = (obj, arr) => - Object - .keys(obj) - .filter((v, i) => arr.indexOf(v) !== -1 ) - .reduce((acc, cur, i) => { - acc[cur] = obj[cur]; - return acc; - }, {}); +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 } From 383d7a57e4cfd712ff0a640aa77f7a189e55fcbd Mon Sep 17 00:00:00 2001 From: King Date: Wed, 13 Dec 2017 17:51:28 -0500 Subject: [PATCH 3/3] ran| npm run build-list --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index b77013e78..b5aef54c6 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ * [Median of array of numbers](#median-of-array-of-numbers) * [Object from key value pairs](#object-from-key-value-pairs) * [Percentile](#percentile) +* [Pick](#pick) * [Pipe](#pipe) * [Powerset](#powerset) * [Promisify](#promisify) @@ -455,6 +456,20 @@ const percentile = (arr, val) => // percentile([1,2,3,4,5,6,7,8,9,10], 6) -> 55 ``` +### 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. + +```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 } + +// pick(object, ['a', 'c'])['a'] -> 1 +// pick(object, ['a', 'c'])['c'] -> 3 + +``` ### Pipe Use `Array.reduce()` to pass value through functions.