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

Merge two or more arrays of JSON objects into one array, preserving uniqueness

本文关键字:数组 一个 保留 唯一性 合并 JSON 两个 对象      更新时间:2023-09-26

我有两个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":Object的情况下合并它们?

提前谢谢。

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

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

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

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

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