2
我在JSON像兩個數組:如何在JavaScript中合併兩個嵌套數組的對象?
proj1 proj1
taska taska
charge1 charge1
charge2 taskb
taskb charge1
charge1 charge2
proj2 proj2
taska taska
taskb charge1
charge1 taskb
charge1
taskc
proj3
taska
,我想他們合併成類似:
proj1
taska
charge1
charge2
taskb
charge1
charge2
proj2
taska
charge1
taskb
charge1
taskc
proj3
taska
我使用jQuery的extend
方法遞歸合併嘗試過,但失敗因爲我需要數組來合併,而extend
取而代之。我是否需要遍歷每個列表的每個級別,或者是否有像extend
這樣的更優雅的解決方案,它合併對象和數組?這裏
編輯是我的名單之一的JSON例子:
{
"projects": [
{
"projectName": "JobIdTooManyChar",
"tasks": [
{
"name": "Process Graphics",
"charges": [
{
"date": "2012-06-18",
"hours": 1
},
{
"date": "2012-06-28",
"hours": "5"
}
]
},
{
"name": "Custom Software",
"charges": [
{
"date": "2012-06-18",
"hours": 1
},
{
"date": "2012-06-19",
"hours": 24
}
]
},
{
"name": "Consulting",
"charges": [
{
"date": "2012-06-18",
"hours": 1
},
{
"date": "2012-06-21",
"hours": 1
}
]
}
]
},
{
"projectName": "JobIdTooManyChar",
"tasks": [
{
"name": "CAD Drawings",
"charges": [
{
"date": "2012-06-18",
"hours": 5
},
{
"date": "2012-06-19",
"hours": 6
},
{
"date": "2012-06-21",
"hours": 9
}
]
}
]
}
]
}
http://api.jquery.com/jQuery.merge/並在需要時遞歸。 – TheZ
這是什麼樣的數組?你可以用JSON發佈它的結構嗎? –
@SalmanA更新了一個詳細的JSON示例。 – xdumaine