将两个或多个JSON对象数组合并到一个数组中,保留唯一性

时间:2016-02-17 08:02:52

标签: javascript jquery arrays json merge

我有两个JSON对象数组,我试图按日期将它们合并到一个数组中,而不创建任何重复项。 jQuery的extend()函数似乎不适合我。我意识到可以使用嵌套的$ .each语句,但是这里关注的数据会变得非常大,所以我宁愿避免使用O(Log N * Log M)......

[  
   {  
      "date":"2016-03-16",
      "timesOff":[
         "18:00 - 20:00",
         "20:00 - 22:00"
      ],
      "appointments":[  
         {  
            "projectId":"adbc5010-ea7d-4993-b442-24cce609c3f8",
            "customerName":"Johnny",
            "timeSlot":"10:00 - 12:00",
            "startTime":""
         },
         {  
            "projectId":"60e0bed4-141b-46f0-91cd-f570fb1f886d",
            "customerName":"Jimmy",
            "timeSlot":"14:00 - 16:00",
            "startTime":""
         }
      ]
   },
   {  
      "date":"2016-03-02",
      "timesOff":[
         "10:00 - 12:00",
         "14:00 - 16:00"
      ],
      "appointments":[  
         {  
            "projectId":"041b6496-4905-42b3-8057-87dc2b8c482a",
            "customerName":"Billy",
            "timeSlot":"08:00 - 10:00",
            "startTime":""
         },
         {  
            "projectId":"f6e0743a-e714-4c92-be63-a14898ec1e4d",
            "customerName":"Bob",
            "timeSlot":"10:00 - 12:00",
            "startTime":""
         }
      ]
   }
]

[
   {  
      "date":"2016-03-16",
      "timesOff":[  
         "14:00 - 16:00",
         "18:00 - 20:00"
      ],
      "appointments":[  
         {  
            "projectId":"041b6496-4905-42b3-8057-87dc2b8c482a",
            "customerName":"Billy",
            "timeSlot":"08:00 - 10:00",
            "startTime":""
         },
         {  
            "projectId":"f6e0743a-e714-4c92-be63-a14898ec1e4d",
            "customerName":"Bob",
            "timeSlot":"10:00 - 12:00",
            "startTime":""
         }
      ]
   },
   {  
      "date":"2016-03-02",
      "timesOff":[  
         "18:00 - 20:00",
         "20:00 - 22:00"
      ],
      "appointments":[  
         {  
            "projectId":"041b6496-4905-42b3-8057-87dc2b8c482a",
            "customerName":"Billy",
            "timeSlot":"08:00 - 10:00",
            "startTime":""
         },
         {  
            "projectId":"f6e0743a-e714-4c92-be63-a14898ec1e4d",
            "customerName":"Bob",
            "timeSlot":"10:00 - 12:00",
            "startTime":""
         }
      ]
   }
]

这些应合并如下:

[  
   {  
      "date":"2016-03-16",
      "timesOff":[
         "14:00 - 16:00",
         "18:00 - 20:00",
         "20:00 - 22:00"
      ],
      "appointments":[  
         {  
            "projectId":"041b6496-4905-42b3-8057-87dc2b8c482a",
            "customerName":"Billy",
            "timeSlot":"08:00 - 10:00",
            "startTime":""
         },
         {  
            "projectId":"f6e0743a-e714-4c92-be63-a14898ec1e4d",
            "customerName":"Bob",
            "timeSlot":"10:00 - 12:00",
            "startTime":""
         },
         {  
            "projectId":"adbc5010-ea7d-4993-b442-24cce609c3f8",
            "customerName":"Johnny",
            "timeSlot":"10:00 - 12:00",
            "startTime":""
         },
         {  
            "projectId":"60e0bed4-141b-46f0-91cd-f570fb1f886d",
            "customerName":"Jimmy",
            "timeSlot":"14:00 - 16:00",
            "startTime":""
         }
      ]
   },
   {  
      "date":"2016-03-02",
      "timesOff":[
         "10:00 - 12:00",
         "14:00 - 16:00",
         "20:00 - 22:00"
      ],
      "appointments":[  
         {  
            "projectId":"041b6496-4905-42b3-8057-87dc2b8c482a",
            "customerName":"Billy",
            "timeSlot":"08:00 - 10:00",
            "startTime":""
         },
         {  
            "projectId":"f6e0743a-e714-4c92-be63-a14898ec1e4d",
            "customerName":"Bob",
            "timeSlot":"10:00 - 12:00",
            "startTime":""
         },
         {  
            "projectId":"041b6496-4905-42b3-8057-87dc2b8c482a",
            "customerName":"Billy",
            "timeSlot":"08:00 - 10:00",
            "startTime":""
         },
         {  
            "projectId":"f6e0743a-e714-4c92-be63-a14898ec1e4d",
            "customerName":"Bob",
            "timeSlot":"10:00 - 12:00",
            "startTime":""
         }
      ]
   }
]

我首先想到的方法是同时在两个数组上分别运行$ .each,然后将值分配给临时变量(即x [value.date] = value)然后对两者运行$ .extend他们这是有效的,但它返回一个像[“2016-03-02”:Object,“2016-03-16”:Object]这样的数组,它不适用于应用程序。如何在没有“Something”的情况下合并这些:对象?

提前致谢。

1 个答案:

答案 0 :(得分:1)

我认为你需要两个phpjs工具。

http://phpjs.org/functions/array_merge/(合并数组)

OR

http://phpjs.org/functions/array_merge_recursive(使用递归模式合并数组,我认为这是你的要求)

http://phpjs.org/functions/array_unique(删除数组中的重复元素和空元素)

合并merge + unique,您将获得预期的结果。只需复制并粘贴函数定义,然后使用它们