9

I would like to combine topData and bottomData into completeData.

var topData = {
    "auth": "1vmPoG22V3qqf43mPeMc",
    "property" : "ATL-D406",  
    "status" : 1,
    "user" : "[email protected]",
    "name" : "Abraham Denson"
}

var bottomData = {
    "agent" : "[email protected]",
    "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?

0

3 Answers 3

8

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" : "[email protected]",
    "name" : "Abraham Denson"
}

var bottomData = {
    "agent" : "[email protected]",
    "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);

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

1 Comment

That did the trick, thanks
3

You can use Object.assign.

var topData = {
  "auth": "1vmPoG22V3qqf43mPeMc",
  "property": "ATL-D406",
  "status": 1,
  "user": "[email protected]",
  "name": "Abraham Denson"
}

var bottomData = {
  "agent": "[email protected]",
  "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

2 Comments

This will mutate topData, which is probably not what the OP wants.
Yep, that did it :)
0
var completeData = {...topData, ...bottomData};

This is object spread syntax.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.