1

Say I have the following two objects:

var a = {
  "abc": {
    "111": {
       name: "jon"
    }
  },
  "xyz": {
    "222": {
       name: "bill"
    }
  }
}

var b = {
  "xyz": {
    "333": {
       name: "mary"
    }
  }
}

I then want to create a new object to merge the two by the top level key name. So to get something like this:

var c = {
  "abc": {
    "111": {
       name: "jon"
    }
  },
  "xyz": {
    "222": {
       name: "bill"
    },
    "333": {
       name: "mary"
    }
  }
}

I imagine it's probably something you do with .map(), .each(), or .reduce()? Would jQuery be helpful in this case? if so how?

4
  • 2
    Why you think you need jQuery here ? Commented Sep 30, 2019 at 18:22
  • @CodeManiac, I actually think it's not needed, but perhaps I am unaware of some jQuery function that would save time. Just wanted to mention it because I use jQuery. Commented Sep 30, 2019 at 18:23
  • This can be simply achieved with native JS, if you're using loadash then you can expect already built function for such cases, but IMO there's no need of using a complete library where as you can do it just simple loops, If you're already using then it's fine, let me see if there's any such function available in jQuery Commented Sep 30, 2019 at 18:25
  • There are two function extend and merge but none of them can be used directly for such case, you need to loop through the keys and than merge them Commented Sep 30, 2019 at 18:33

3 Answers 3

4

You can simply use Object.entries() and Array.reduce(),

The main idea is to iterate over both the arrays and maintain a map.

  • Iterate over the first object and create a map of all of its properties.

  • Than iterate over the second object, and for each of its properties check if it is present in map or not, if it is present than merge the two, If not than simply add it.

var a = { "abc": { "111": { name: "jon" } }, "xyz": { "222": { name: "bill" } } }
var b = { "xyz": { "333": { name: "mary" } } };

let result = Object.entries(a).reduce((acc,[key, value]) => (acc[key] = value,acc),{});
Object.entries(b).reduce((acc,[key, value]) => (acc[key] =acc[key] && Object.assign(acc[key], value)|| value,acc),result);

console.log(result);

In case you can use one of your existing objects than a simple solution can be:

var a = { "abc": { "111": { name: "jon" } }, "xyz": { "222": { name: "bill" } } }
var b = { "xyz": { "333": { name: "mary" } } };

Object.entries(a).forEach(([key, value])=>{
  b[key] = Object.assign(b[key] || {}, value);
});

console.log(b);

Sign up to request clarification or add additional context in comments.

1 Comment

I'm upvoting since this answers the question and is a clean solution, but it would be great to see some explanation on why this code solves the question (since the code is very reduced, it turns out hard to understand by just reading the code)
2

If you're already using jQuery or Lodash in your project you can use $.extend or _.merge respectively. There is no need to reinvent the wheel when you're already using one of those libraries.

var a = { "abc": { "111": { name: "jon" } }, "xyz": { "222": { name: "bill" } } };
var b = { "xyz": { "333": { name: "mary" } } };

console.log($.extend(true, {}, a, b));
console.log(_.merge({}, a, b));
console.log("a =", a);
console.log("b =", b);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

2 Comments

I can only accept one answer, but this answer is also correct and useful.
@Ben np, upvoting is enough to mark an answer as helpful.
0

could you use the es6 object destructuring?

var a = {
  "abc": {
    "111": {
       name: "jon"
    }
  },
  "xyz": {
    "222": {
       name: "bill"
    }
  }
}

var b = {
  "xyz": {
    "333": {
       name: "mary"
    }
  }
}

   const c = {abc: a.abc, xyz: [a.xyz, b.xyz]};
    console.log(c);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.