My goal is to compare the dates from 2 separate arrays and extract the names that match, pushing them into a new array. For any missing dates, I want to push '0' instead.
This is my current approach:
var data = [{
name: 'Amy',
date: '2020-01-01'
}, {
name: 'John',
date: '2020-01-02'
}, {
name: 'Sara',
date: '2020-01-04'
}];
var fulldate = ['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-04', '2020-01-05'];
var newData = [];
var len = data.length;
for (var i = 0; i < len; i++) {
if (data[i].date == fulldate[i]) {
newData.push(data[i].name);
} else if (data[i].date != fulldate[i]) {
newData.push("0")
}
}
console.log(newData);
The issue arises when encountering an unmatched date as it halts the process:
Amy,John,0
This is what I am aiming for:
Amy, John, 0, Sara, 0