I am struggling to combine two sets of data into a single array by matching them based on the day. I want to create a new array of objects that include the relevant information from both sets. The first set includes the following:
[
{ date: '9/30/2017', day: 'Saturday' },
{ date: '10/1/2017', day: 'Sunday' },
{ date: '10/2/2017', day: 'Monday' },
{ date: '10/3/2017', day: 'Tuesday' },
{ date: '10/4/2017', day: 'Wednesday' },
{ date: '10/5/2017', day: 'Thursday' },
{ date: '10/6/2017', day: 'Friday' },
{ date: '10/7/2017', day: 'Saturday' },
{ date: '10/8/2017', day: 'Sunday' },
{ date: '10/9/2017', day: 'Monday' },
{ date: '10/10/2017', day: 'Tuesday' }
]
and
[
{
day: 'Sunday',
totalHours: 10,
serveTimes: [ { startTime: 5, stopTime: 15 } ]
},
{
day: 'Monday',
totalHours: 10,
serveTimes: [ { startTime: 5, stopTime: 10 }, { startTime: 15, stopTime: 20 } ]
},
{
day: 'Tuesday',
totalHours: 3,
serveTimes: [ { startTime: 2, stopTime: 5 } ]
},
{
day: 'Wednesday',
totalHours: 10,
serveTimes: [ { startTime: 5, stopTime: 15 } ]
},
{
day: 'Thursday',
totalHours: 5,
serveTimes: [ { startTime: 5, stopTime: 7 }, { startTime: 20, stopTime: 23 } ]
},
{
day: 'Friday',
totalHours: 5,
serveTimes: [ { startTime: 5, stopTime: 10 } ]
},
{
day: 'Saturday',
totalHours: 10,
serveTimes: [ { startTime: 5, stopTime: 10 }, { startTime: 13, stopTime: 18 } ]
}
]
This was the code I used:
function finalFormat (a, d) {
var combined = a.concat(d)
var final = _(combined)
.groupBy('day')
.map(_.spread(_.assign))
.value()
return final
}
However, the resulting array is missing data for some dates as shown below:
[...]
It lacks information for all dates, and I'm feeling stuck. Can someone offer guidance?