[英]Append object to child object in javascript
我有以下對象:
var dpsets = {
"routes": [
{
"name": "first",
"values": [
"info",
"more info",
"text"
]
},
{
"name": "second",
"values": [
"text again",
"with info"
]
}
]
}
我還有以下對象:
var currentSet = {
"name": "New Route",
"values": [
"Latest one",
"Latest two"
]
}
我試圖將currentSet
追加到dpsets
的routes
元素。
我已經嘗試了dpsets.routes.push(currentSet)
和jQuery的$.extend(dplocations.routes, currentRoute)
但似乎都沒有達到我的要求。
最好的方法是什么?
本來這不起作用:
dpsets.routes.push(currentSet)
但是,再次嘗試后,效果很好。
var dpsets = {
"routes": [
{
"name": "first",
"values": [
"info",
"more info",
"text"
]
},
{
"name": "second",
"values": [
"text again",
"with info"
]
}
]
};
var currentSet = {
"name": "New Route",
"values": [
"Latest one",
"Latest two"
]
};
dpsets.routes.push(currentSet);
console.log(dpsets);
您是否嘗試使用方法concat()?
dpsets.concat(currentSet)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.