8

I would like to combine topData and bottomData into completeData.

var topData = {
    "auth": "1vmPoG22V3qqf43mPeMc",
    "property" : "ATL-D406",  
    "status" : 1,
    "user" : "test001@aaa.com",
    "name" : "Abraham Denson"
}

var bottomData = {
    "agent" : "pusher@agent.com",
    "agency" : "Thai Tims Agency",
    "agentCommission" : 1000,
    "arrival" : "arrive 12pm at condo",
    "departure" : "leaving room at 6pm",
}

var completeData = topData.concat(bottomData)

Since these are not arrays, concat wont work here.

Can this be done without making foreach loops?

torbenrudgaard
  • 2,375
  • 7
  • 32
  • 53

3 Answers3

7

You can use Object.assign() to concatenate your objects.

var newObj = Object.assign({}, topData, bottomData)

From MDN:

The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object.


var topData = {
    "auth": "1vmPoG22V3qqf43mPeMc",
    "property" : "ATL-D406",  
    "status" : 1,
    "user" : "test001@aaa.com",
    "name" : "Abraham Denson"
}

var bottomData = {
    "agent" : "pusher@agent.com",
    "agency" : "Thai Tims Agency",
    "agentCommission" : 1000,
    "arrival" : "arrive 12pm at condo",
    "departure" : "leaving room at 6pm",
}

var completeData = Object.assign({}, topData, bottomData);

console.log(completeData);
Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
2

You can use Object.assign.

var topData = {
  "auth": "1vmPoG22V3qqf43mPeMc",
  "property": "ATL-D406",
  "status": 1,
  "user": "test001@aaa.com",
  "name": "Abraham Denson"
}

var bottomData = {
  "agent": "pusher@agent.com",
  "agency": "Thai Tims Agency",
  "agentCommission": 1000,
  "arrival": "arrive 12pm at condo",
  "departure": "leaving room at 6pm",
}

var completeData = Object.assign(topData, bottomData);
console.log(completeData)
It return the target object which mean properties from bottomData will be added to topData
brk
  • 48,835
  • 10
  • 56
  • 78
0
var completeData = {...topData, ...bottomData};

This is object spread syntax.