0

I want to append an object of objects to another object of objects like so:

object1: { item1: {}, item2: {} } to object2: { item3: {}, item4: {} } 

So that the outcome is this:

object1: { item1: {}, item2: {}, item3: {}, item4: {} } 

I need to do this in javascript and can also use lodash as an option.

1
  • 3
    Before asking trivial questions such as this one, please read the docs, search using google, and then search again (research). Commented Aug 5, 2016 at 14:14

1 Answer 1

3

Use Object.assign() function:

Object.assign(object1, object2); 

Also, to make in portable, you can use lodash lib:

_.assign(object1, object2); 

If you want to create new object, instead of mutating existing one, you can provide {} as first argument:

_.assign({}, object1, object2); 
Sign up to request clarification or add additional context in comments.

3 Comments

This won't work in IE, and isn't very portable.
They wanted to mutate the properties of object1. Don't use the {}
@4castle, thanks, I'll update answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.