我在 JSON 中有两个数组,例如:
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
}
]
}
]
}
]
}