From 7c1d156631d1e414e884abf92ba5d613e482f1e9 Mon Sep 17 00:00:00 2001 From: 30secondsofcode <30secondsofcode@gmail.com> Date: Fri, 12 Jan 2018 12:48:24 +0000 Subject: [PATCH] Travis build: 1213 --- README.md | 41 +++++++++++++++++++++++++++++++++++++++++ docs/index.html | 21 ++++++++++++++++++++- snippets/merge.md | 8 +++----- 3 files changed, 64 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 9c32997bd..0dc34c6d2 100644 --- a/README.md +++ b/README.md @@ -266,6 +266,7 @@ average(1, 2, 3); * [`lowercaseKeys`](#lowercasekeys) * [`mapKeys`](#mapkeys) * [`mapValues`](#mapvalues) +* [`merge`](#merge) * [`objectFromPairs`](#objectfrompairs) * [`objectToPairs`](#objecttopairs) * [`orderBy`](#orderby) @@ -4071,6 +4072,46 @@ mapValues(users, u => u.age); // { fred: 40, pebbles: 1 }
[⬆ Back to top](#table-of-contents) +### merge + +Creates a new object from the combination of two or more objects. + +Use `Array.reduce()` combined with `Object.keys(obj)` to iterate over all objects and keys. +Use `hasOwnProperty()` and `Array.concat()` to append values for keys existing in multiple objects. + +```js +const merge = (...objs) => + [...objs].reduce( + (acc, obj) => + Object.keys(obj).reduce((a, k) => { + acc[k] = acc.hasOwnProperty(k) ? [].concat(acc[k]).concat(obj[k]) : obj[k]; + return acc; + }, {}), + {} + ); +``` + +
+Examples + +```js +const object = { + a: [{ x: 2 }, { y: 4 }], + b: 1 +}; +const other = { + a: { z: 3 }, + b: [2, 3], + c: 'foo' +}; +merge(object, other); // { a: [ { x: 2 }, { y: 4 }, { z: 3 } ], b: [ 1, 2, 3 ], c: 'foo' } +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### objectFromPairs Creates an object from the given key-value pairs. diff --git a/docs/index.html b/docs/index.html index 6f54b83cb..89d0fe0d1 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,7 @@ scrollToTop(); } }, false); - }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
+      }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
 
Promise.resolve([1, 2, 3])
   .then(call('map', x => 2 * x))
   .then(console.log); //[ 2, 4, 6 ]
@@ -890,6 +890,25 @@ Foo.prototype: { user: 'pebbles', age: 1 }
 };
 mapValues(users, u => u.age); // { fred: 40, pebbles: 1 }
+

merge

Creates a new object from the combination of two or more objects.

Use Array.reduce() combined with Object.keys(obj) to iterate over all objects and keys. Use hasOwnProperty() and Array.concat() to append values for keys existing in multiple objects.

const merge = (...objs) =>
+  [...objs].reduce(
+    (acc, obj) =>
+      Object.keys(obj).reduce((a, k) => {
+        acc[k] = acc.hasOwnProperty(k) ? [].concat(acc[k]).concat(obj[k]) : obj[k];
+        return acc;
+      }, {}),
+    {}
+  );
+
const object = {
+  a: [{ x: 2 }, { y: 4 }],
+  b: 1
+};
+const other = {
+  a: { z: 3 },
+  b: [2, 3],
+  c: 'foo'
+};
+merge(object, other); // { a: [ { x: 2 }, { y: 4 }, { z: 3 } ], b: [ 1, 2, 3 ], c: 'foo' }
 

objectFromPairs

Creates an object from the given key-value pairs.

Use Array.reduce() to create and combine key-value pairs.

const objectFromPairs = arr => arr.reduce((a, v) => ((a[v[0]] = v[1]), a), {});
 
objectFromPairs([['a', 1], ['b', 2]]); // {a: 1, b: 2}
 

objectToPairs

Creates an array of key-value pair arrays from an object.

Use Object.keys() and Array.map() to iterate over the object's keys and produce an array with key-value pairs.

const objectToPairs = obj => Object.keys(obj).map(k => [k, obj[k]]);
diff --git a/snippets/merge.md b/snippets/merge.md
index fbc4ec7b7..b20d05253 100644
--- a/snippets/merge.md
+++ b/snippets/merge.md
@@ -10,9 +10,7 @@ const merge = (...objs) =>
   [...objs].reduce(
     (acc, obj) =>
       Object.keys(obj).reduce((a, k) => {
-        acc[k] = acc.hasOwnProperty(k)
-          ? [].concat(acc[k]).concat(obj[k])
-          : obj[k];
+        acc[k] = acc.hasOwnProperty(k) ? [].concat(acc[k]).concat(obj[k]) : obj[k];
         return acc;
       }, {}),
     {}
@@ -22,12 +20,12 @@ const merge = (...objs) =>
 ```js
 const object = {
   a: [{ x: 2 }, { y: 4 }],
-  b: 1,
+  b: 1
 };
 const other = {
   a: { z: 3 },
   b: [2, 3],
-  c: 'foo',
+  c: 'foo'
 };
 merge(object, other); // { a: [ { x: 2 }, { y: 4 }, { z: 3 } ], b: [ 1, 2, 3 ], c: 'foo' }
 ```