简体   繁体   English

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

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

I have two arrays of JSON objects, I am trying to merge them into one array, by date, without creating any duplicates. 我有两个JSON对象数组,我试图按日期将它们合并到一个数组中,而不创建任何重复项。 jQuery's extend() function does not seem to do the trick for me. jQuery的extend()函数似乎对我没有帮助。 I realize it's possible to use nested $.each statements, but the data in focus here can become very large, so I would rather avoid O(Log N * Log M)... 我意识到可以使用嵌套的$ .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":""
         }
      ]
   }
]

These should be merged like so: 这些应该像这样合并:

[  
   {  
      "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":""
         }
      ]
   }
]

My first thought of approach was to run $.each on both of the arrays separately at the same time, then assign values to temporary variables (ie x[value.date] = value) and then run $.extend against both of them. 我首先想到的方法是同时在两个数组上分别运行$ .each,然后将值分配给临时变量(即x [value.date] = value),然后对这两个数组运行$ .extend。 This works, however it returns an array like ["2016-03-02":Object, "2016-03-16":Object], which will not work for the purpose of the application. 此方法有效,但是它会返回一个类似于[“ 2016-03-02”:Object,“ 2016-03-16”:Object]的数组,该数组不适用于该应用程序。 How can I merge these without the "Something":Object? 没有“ Something”:Object,如何合并它们?

Thanks in advance. 提前致谢。

I think you need two phpjs tools. 我认为您需要两个phpjs工具。

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

OR 要么

http://phpjs.org/functions/array_merge_recursive (merge arrays with recursive mode, I think it's your requirement) http://phpjs.org/functions/array_merge_recursive (将数组与递归模式合并,我认为这是您的要求)

AND

http://phpjs.org/functions/array_unique (delete duplicated and empty elements in array) http://phpjs.org/functions/array_unique (删除数组中重复的元素和空元素)

Combine merge + unique and you'll obtain the expected results. 结合合并+唯一,您将获得预期的结果。 Just copy and paste the function definition and then use them 只需复制并粘贴函数定义,然后使用它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM