1

I want to combine the content of multiple objects that have the same key with that key as the property in JavaScript.

Objects:

const cat1 = { med1: { a: 10, b: 12 }, med2: { c: 14, d: 16 } };

const cat2 = { med1: { e: 18, f: 20 }, med2: { g: 22, h: 24 } };

Expected output:

{
   med1: { a: 10, b: 12, e: 18, f: 20 }, 
   med2: { c: 14, d: 16, g: 22, h: 24 }
}

I have tried to use both object spreading and Object.assign with no sucess. With object spreading, since objects have no iterator this is returning an error. Object.assign would work however since the two objects to combine have the same key, the second object is overwriting the first.

bienvenu
  • 33
  • 4
  • 4
    You cannot have two `a` keys in the same object. – juzraai May 31 '22 at 06:41
  • 1
    no wonder you couldn't write any code since your expected output is impossible output – Bravo May 31 '22 at 06:44
  • You're right, didn't notice that... however I have updated the question and would appreciate any help. – bienvenu May 31 '22 at 07:50
  • Does this answer your question? [How can I merge properties of two JavaScript objects dynamically?](https://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically) – juzraai May 31 '22 at 08:16
  • 1/2 ... Since the matter is about merge strategies, how does the OP want to handle name clashes. If it comes to overwriting which object's property does win. When merging property values other than arrays and objects how does one handle it (e.g. summing up number values? string/array concatenation? Or collecting/pushing them in/to arrays?). How are array values going to be handled? – Peter Seliger May 31 '22 at 08:25
  • 2/2 ... Insights into how complex it can/might get are e.g. provided with ... [_"Comparing 2 nested data-structures,target+source,what are appropriate merge-strategies for missing target values compared to their source counterpart?"_](https://stackoverflow.com/questions/72286105/comparing-2-nested-data-structures-targetsource-what-are-appropriate-merge-stra/72289681#72289681) – Peter Seliger May 31 '22 at 08:30

2 Answers2

0

You can iterate over the object keys and create a new object out of their aggregation.

You can use ES6 spread operator (...) which allows us to quickly copy all or part of an existing array or object into another array or object.

const cat1 = { med1: { a: 10, b: 12 }, med2: { c: 14, d: 16 } };
const cat2 = { med1: { e: 18, f: 20 }, med2: { g: 22, h: 24 } };
let resultObject = {};
Object.keys(cat1).map(key => { // iterate over the keys
  resultObject = {
    ...resultObject,
    [key]: {...cat1[key], ...cat2[key]} // merge two objects 
  }
  return;
});

console.log(resultObject);
subodhkalika
  • 1,964
  • 1
  • 9
  • 15
0

For combining two objects with a single level of indirection

const cat1 = { med1: { a: 10, b: 12 }, med2: { c: 14, d: 16 } }
const cat2 = { med1: { e: 18, f: 20 }, med2: { g: 22, h: 24 } }

const merge = (o1, o2) =>
    Object.keys(o1)
        .reduce((p, key) => 
            (p[key] = { ...o1[key], ...o2[key] }, p), {})

console.log(merge(cat1, cat2))
Ben Aston
  • 53,718
  • 65
  • 205
  • 331