我有一個數據集,看起來像這樣:如何在javascript/jquery中合併兩個json對象?
{
"Stops": [
{
"Name": "COLUMBIA RD NW + MINTWOOD PL NW",
"Routes": [
"42",
"43",
"H1",
"L2"
]
}
]
}
,另一個是這樣的:
{
"Predictions": [
{
"DirectionText": "North to Mt Pleasant Via Adams Morgan",
"Minutes": 7,
"RouteID": "42"
},
{
"DirectionText": "North to Mt Pleasant Via Adams Morgan",
"Minutes": 25,
"RouteID": "42"
},
{
"DirectionText": "North to Chevy Chase Circle",
"Minutes": 32,
"RouteID": "L2"
},
{
"DirectionText": "North to Mt Pleasant Via Adams Morgan",
"Minutes": 36,
"RouteID": "42"
},
{
"DirectionText": "North to Mt Pleasant Via Adams Morgan",
"Minutes": 58,
"RouteID": "42"
},
{
"DirectionText": "North to Mt Pleasant Via Adams Morgan",
"Minutes": 69,
"RouteID": "42",
}
],
"StopName": "Columbia Rd Nw + Mintwood Pl Nw"
}
我想結合有關第一組路線ID的數據,具體添加要設置的分鐘數。所以輸出會是這樣的:
{
"Stops": [
{
"Name": "COLUMBIA RD NW + MINTWOOD PL NW",
"Routes": [
{
"42": [
{
"Minutes": [7, 25,36,58,69]
}
],
"43": [
{
"Minutes": []
}
],
"H1":[
{
"Minutes": []
}
],
"L2": [
{
"Minutes": [32]
}
]
}
],
"StopID": "1001779"
}
]
}
我堅持如何寫一些JavaScript/jQuery將做到這一點合併。有任何想法嗎?
[** $擴展()**(http://api.jquery.com/jQuery .extend /) – adeneo
@adeneo我懷疑$ .extend()會完成這項工作。我猜這裏的'for'循環是正確的。 – VisioN
哪裏有數據集?如果它是JSON,那麼你需要以某種方式讀取它們,並將它們解析爲Javascript對象。你需要將結果作爲Javascript對象還是JSON? – Guffa