I have two arrays of JSON objects, I am trying to merge them into one array, by date, without creating any duplicates. jQuery's extend() function does not seem to do the trick for me. 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)...
[
{
"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 (i.e. x[value.date] = value) and then run $.extend against both of them. 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. How can I merge these without the "Something":Object?
Thanks in advance.