From 4d0e26f106b4d2ee65f672fa824adf810858588f Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 23 Mar 2020 15:06:47 +0200 Subject: [PATCH 1/3] Update objectToPairs snippet --- snippets/objectToPairs.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/snippets/objectToPairs.md b/snippets/objectToPairs.md index 9e241a96e..e1ff7260e 100644 --- a/snippets/objectToPairs.md +++ b/snippets/objectToPairs.md @@ -5,12 +5,12 @@ tags: object,array,beginner Creates an array of key-value pair arrays from an object. -Use `Object.keys()` and `Array.prototype.map()` to iterate over the object's keys and produce an array with key-value pairs. +Use `Object.entries()` to get an array of key-value pair arrays from the given object. ```js -const objectToPairs = obj => Object.keys(obj).map(k => [k, obj[k]]); +const objectToPairs = obj => Object.entries(obj); ``` ```js objectToPairs({ a: 1, b: 2 }); // [ ['a', 1], ['b', 2] ] -``` \ No newline at end of file +``` From edba0e04600da83312bf61c1349a24128ad4f603 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 23 Mar 2020 15:07:10 +0200 Subject: [PATCH 2/3] Add objectToEntries snippet --- snippets_archive/objectToEntries.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 snippets_archive/objectToEntries.md diff --git a/snippets_archive/objectToEntries.md b/snippets_archive/objectToEntries.md new file mode 100644 index 000000000..90d619f09 --- /dev/null +++ b/snippets_archive/objectToEntries.md @@ -0,0 +1,16 @@ +--- +title: objectToEntries +tags: object,array,beginner +--- + +Creates an array of key-value pair arrays from an object. + +Use `Object.keys()` and `Array.prototype.map()` to iterate over the object's keys and produce an array with key-value pairs. + +```js +const objectToEntries = obj => Object.keys(obj).map(k => [k, obj[k]]); +``` + +```js +objectToEntries({ a: 1, b: 2 }); // [ ['a', 1], ['b', 2] ] +``` From 749920ae7f40e68de50735cba20817849ca31e2f Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 23 Mar 2020 15:07:23 +0200 Subject: [PATCH 3/3] Add toPairs snippet --- snippets/toPairs.md | 23 +++++++++++++++++++++++ test/toPairs.test.js | 17 +++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 snippets/toPairs.md create mode 100644 test/toPairs.test.js diff --git a/snippets/toPairs.md b/snippets/toPairs.md new file mode 100644 index 000000000..832f81f51 --- /dev/null +++ b/snippets/toPairs.md @@ -0,0 +1,23 @@ +--- +title: toPairs +tags: object,array,intermediate +--- + +Creates an array of key-value pair arrays from an object or other iterable (object, array, string, set etc.). + +Check if `Symbol.iterator` is defined and, if so, use `Array.prototype.entries()` to get an iterator for the given iterable, `Array.from()` to convert the result to an array of key-value pair arrays. +If `Symbol.iterator` is not defined for `obj`, use `Object.entries()` instead. + +```js +const toPairs = obj => + obj[Symbol.iterator] instanceof Function && obj.entries instanceof Function + ? Array.from(obj.entries()) + : Object.entries(obj); +``` + +```js +toPairs({ a: 1, b: 2 }); // [ ['a', 1], ['b', 2] ] +toPairs([2, 4, 8]); // [ [0, 2], [1, 4], [2, 8] ] +toPairs('shy'); // [ ['0', 's'], ['1', 'h'], ['2', 'y'] ] +toPairs(new Set(['a', 'b', 'c', 'a'])); // [ ['a', 'a'], ['b', 'b'], ['c', 'c'] ] +``` diff --git a/test/toPairs.test.js b/test/toPairs.test.js new file mode 100644 index 000000000..f534526f2 --- /dev/null +++ b/test/toPairs.test.js @@ -0,0 +1,17 @@ +const {toPairs} = require('./_30s.js'); + +test('toPairs is a Function', () => { + expect(toPairs).toBeInstanceOf(Function); +}); +test('Creates an array of key-value pair arrays from an object.', () => { + expect(toPairs({ a: 1, b: 2 })).toEqual([['a', 1], ['b', 2]]); +}); +test('Creates an array of key-value pair arrays from an array.', () => { + expect(toPairs([2, 4, 8])).toEqual([[0, 2], [1, 4], [2, 8]]); +}); +test('Creates an array of key-value pair arrays from a string.', () => { + expect(toPairs('shy')).toEqual([['0', 's'], ['1', 'h'], ['2', 'y']]); +}); +test('Creates an array of key-value pair arrays from a set.', () => { + expect(toPairs(new Set(['a', 'b', 'c', 'a']))).toEqual([['a', 'a'], ['b', 'b'], ['c', 'c']]); +});