Success Man Success Man -3 years ago 121
jQuery Question

How can I combine two object in javascript?

my first object like this :

{id: 1, quantity: 10, address: {id: 2, name: "stamford bridge", city: "london"}}


my second object like this :

{data: {id: 2, quantity: 20, address: {id: 4, name: "old traford", city: "manchester"}}, expired: "2017-08-16T06:46:02.566Z"}


I want to combine the object

How can I do it?

Answer Source

How can I merge properties of two JavaScript objects dynamically?

This method can combine the properties of two objects together.

Object.assign(obj1, obj2);

Or you have to change your data structure, if you're thinking about arrays.

var x = {id: 1, quantity: 10, address: {id: 2, name: "stamford bridge", city: "london"}}

and

var y = {data: [{id: 2, quantity: 20, address: {id: 4, name: "old traford", city: "manchester"}}], expired: "2017-08-16T06:46:02.566Z"}

Then you

y.data.push(x)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download