javascript - 将两个 json 部分组合成一个 json 对象

标签 javascript json

<分区>

我想将 topData 和 bottomData 合并为 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)

因为这些不是数组,concat 在这里不起作用。

这可以在不进行 foreach 循环的情况下完成吗?

最佳答案

您可以使用 Object.assign()连接您的对象。

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

来自 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);

关于javascript - 将两个 json 部分组合成一个 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44993897/

相关文章:

javascript - javascript中是否有一种简单的方法来测试IE8中的 “null or not an object”错误

javascript - React.js 和 JSON : Dynamic Styles?

javascript - 为什么一个 cookie 会存储两次?

javascript - Alertify 对话框在确认前消失

json - Powershell - 使用 ConvertTo-Json 保留所有 Enum 属性的文本

javascript - 带有 d3.json(bardata.php) 的空白网页,但适用于 d3.json(bardata.json)

json - Jinja2 - 如何循环 json 列表?

javascript - 取消为一个 ID 设置的多个超时

ios - 如何在 Swift 中解析 JSON 中的数组?

java - Volley 请求给出的响应不准确,但链接数据正确