【发布时间】:2016-05-28 19:37:33
【问题描述】:
我有两个 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”:对象,如何合并这些?
提前致谢。
【问题讨论】:
-
如果日期相同,其他一切是否都相同,还是需要更深入的测试?这是纯 JS 的任务,我怀疑 jquery 是矫枉过正
-
日期并不总是相同的,事实上可以保证超出此数组范围的其他日期将成为焦点。这样做的底线功能是充当调度程序,保留从 get 请求中保留的信息,然后将它们存储到此数组中以防止额外的 get 请求。生成的数组可能会变得很大。
标签: javascript jquery arrays json merge