如何在javascript中将两个对象与嵌套数组合并?

时间:2012-06-28 19:38:06

标签: jquery arrays

我在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
                        }
                    ]
                }
            ]
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

jQuery和underscore.js都将数组视为属性 value ,因此他们将替换为另一个。

老实说,我相信你只需要编写(扩展他们已经完成的工作)方法来扩展对象,就像你需要它们一样。例如,对于数组,键位置1是否具有特殊含义,还是仅仅是堆栈/队列?

您是否需要帮助编写这样的合并实用程序?