1

I'm not sure how to do this so any help would be appreciated. I have two objects I want to combine into one object. I've used the spread operator to do this:

newObj = {...obj1, ...obj2};

this, for example gives me this:

{
  [
    obj1A{
     "item": "stuff",
     "item": "stuff"
    },
    obj1B{
     "item": "stuff",
     "item": "stuff"
    }
  ],
  [
    obj2A{
     "item": "stuff",
     "item": "stuff"
    },
    obj2B{
     "item": "stuff",
     "item": "stuff"
    }
  ]
}

But what I want is this:

 {
      [
        obj1A{
         "item": "stuff",
         "item": "stuff"
        },
        obj1B{
         "item": "stuff",
         "item": "stuff"
        },
        obj2A{
         "item": "stuff",
         "item": "stuff"
        },
        obj2B{
         "item": "stuff",
         "item": "stuff"
        }
      ]
    }

Anyone know how to do this?

1
  • 5
    please add valid objects. arrays do not have properties in literal syntax. Commented Mar 14, 2018 at 17:44

3 Answers 3

4

With proper unique names, you could use Object.assign and create a new object.

var object1 = { obj1A: { item1: "stuff", item2: "stuff" }, obj1B: { item1: "stuff", item2: "stuff" } },
    object2 = { obj2A: { item1: "stuff", item2: "stuff" }, obj2B: { item1: "stuff", item2: "stuff" } },
    combined = Object.assign({}, object1, object2);
  
console.log(combined);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

5 Comments

Of course if that's the shape of the objects, then the original technique would work just as well here: combined = {...object1, ...object2}. So I suspect there's something else going on here.
@ScottSauyet, actually not in all user agents.
Oh, I never realized that. Is it just old IE?
Thank you. I've only recently gotten back into working with browsers. Too much Node lately; it's awfully convenient to be working with a single evergreen agent!
Accepting this answer as it's correct for the question asked. Though it doesn't solve my problem. I was already using the spread operator, which does the same thing regardless of compatibility. I think I'll post up an other question that's more specific to my issue. Apologies for the generality of the initial question.
0

If you're using jQuery you could do something similar to this https://jsfiddle.net/d11kfd4d/:

var a = { 
   propertyOne: 'One',
   propertyTwo: 'Two'
};

var b = { 
   propertyThree: 'Three',
   propertyFour: 'Four'
}

var c = $.extend(a, b);

console.log(c);

Comments

0

Assuming you have a typo (changing this [] to this {}) and the keys are valids (not repeated), you can use the Spread syntax.

var obj1 = { obj1A: { item: "stuff", item2: "stuff" }, obj1B: { item: "stuff", item2: "stuff" } },
    obj2 = { obj2A: { item: "stuff", item2: "stuff" }, obj2B: { item: "stuff", item2: "stuff" } }
    
console.log({...obj1, ...obj2});
.as-console-wrapper { max-height: 100% !important; top: 0; }

1 Comment

The question noted that this did not work. So I suspect the original objects are not as suggested. (It's not just a bracket change.)

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.