I have the folowing use case and I am stack for days.
I have an array which contains dates in a string format like the following:
var rows = ["23-01-2021", "24-01-2021", "25-01-2021", "26-01-2021", "27-01-2021", "28-01-2021"]
I have also a data structure with the working days for each employee
var schedule = [{
id: 1,
name: "Chandler Bing",
times: [
{
start: "9.00",
end: "17.00",
date: "25-01-2021"
},
{
start: "9.00",
end: "17.00",
date: "26-01-2021"
},
{
start: "9.00",
end: "17.00",
date: "28-01-2021"
},
]
},
{
id: 2,
name: "Employee 2",
times: [
{
start: "9.00",
end: "17.00",
date: "23-01-2021"
},
{
start: "9.00",
end: "17.00",
date: "27-01-2021"
},
{
start: "9.00",
end: "17.00",
date: "28-01-2021"
}
]
}
]
I would like to compare the array of dates with the array of times for each employee. If dates are missing from the array of times create an object with null values... Actually I would like to end up to a data structure like the following.
var schedule = [{
id: 1,
name: "Employee 1",
times: [{
start: "",
end: "",
date: ""
}, {
start: "",
end: "",
date: ""
},
{
start: "9.00",
end: "17.00",
date: "25-01-2021"
},
{
start: "9.00",
end: "17.00",
date: "26-01-2021"
},
{
start: "",
end: "",
date: ""
},
{
start: "9.00",
end: "17.00",
date: "28-01-2020"
}
]
},
{
id: 2,
name: "Employee 2",
times: [
{
start: "",
end: "",
date: ""
},
{
start: "",
end: "",
date: ""
},
{
start: "",
end: "",
date: ""
},
{
start: "",
end: "",
date: ""
},
{
start: "9.00",
end: "17.00",
date: "27-01-2021"
},
{
start: "9.00",
end: "17.00",
date: "28-01-2021"
},
]
}
]]
I have implemented this logic until now but obviosly is not working
var rows = ["23-01-2021", "24-01-2021", "25-01-2021", "26-01-2021", "27-01-2021", "28-01-2021"]
var schedule = [{
id: 1,
name: "Chandler Bing",
times: [{
start: "9.00",
end: "17.00",
date: "25-01-2021"
},
{
start: "9.00",
end: "17.00",
date: "26-01-2021"
},
{
start: "9.00",
end: "17.00",
date: "28-01-2021"
},
]
}]
var arr = []
var test = {};
rows.forEach(function(row, index) {
schedule.forEach(function(entry) {
entry.times.forEach(function(times, index) {
console.log(row)
if (times.date === row) {
test = {
start: times.start,
end: times.end,
date: times.date
}
arr.push(test)
} else {
test = {
start: "",
end: "",
date: ""
}
arr.push(test);
}
})
})
})
console.log(arr);
question from:
https://stackoverflow.com/questions/65901770/compare-the-date-value-of-an-array-of-objects-with-the-dates-from-an-array-of-st