Reputation: 135
I have this key/value object:
{
"march 2021 - monday": [
{
"day": "MONDAY",
"date": "2021-03-01",
"timetables": [
{
"day": "MONDAY",
"slots": [
"09:00 - 09:30",
"09:30 - 10:00",
...
],
"changed": false
},
{
"day": "MONDAY",
"slots": [
"14:00 - 14:30",
"14:30 - 15:00",
...
],
"changed": false
}
]
},
{
"day": "MONDAY",
"date": "2021-03-08",
"timetables": [
{
"day": "MONDAY",
"slots": [
"09:00 - 09:30",
"09:30 - 10:00",
...
],
"changed": false
},
{
"day": "MONDAY",
"slots": [
"14:00 - 14:30",
"14:30 - 15:00",
...
],
"changed": false
}
]
}
],
"march 2021 - tuesday": [
{
"day": "TUESDAY",
"date": "2021-03-09",
"timetables": [
{
"day": "TUESDAY",
"slots": [
"07:00 - 07:30",
"07:30 - 08:00",
...
],
"changed": false
}
]
}
],
"march 2021 - thursday": [
{
"day": "THURSDAY",
"date": "2021-03-18",
"timetables": [
{
"day": "THURSDAY",
"slots": [
"10:00 - 10:30",
"10:30 - 11:00",
...
],
"changed": false
}
]
}
]
}
I would like to:
"changed": true
for at least one timetable for every date
property;I make this codepen where there are a couple of attempts. The second attempt seems to work but I'm not sure. Do you think this is correct? Is there another way to get what I want?
Thanks
Upvotes: 0
Views: 107
Reputation: 33439
I don't know, if I understand return true when there is a "changed": true for at least one timetable for every date property
correctly, but the follwing seems to be a solution.
Your second attempt has the same logic, but lacks a little bit of structure
console.clear()
const portalDays = {
'march 2021 - monday': [
{
day: 'MONDAY',
date: '2021-03-01',
timetables: [
{
day: 'MONDAY',
slots: [
'09:00 - 09:30',
'09:30 - 10:00'
],
changed: false
},
{
day: 'MONDAY',
slots: [
'14:00 - 14:30',
'14:30 - 15:00'
],
changed: false
}
]
},
{
day: 'MONDAY',
date: '2021-03-08',
timetables: [
{
day: 'MONDAY',
slots: [
'09:00 - 09:30',
'09:30 - 10:00'
],
changed: false
},
{
day: 'MONDAY',
slots: [
'14:00 - 14:30',
'14:30 - 15:00'
],
changed: false
}
]
}
],
'march 2021 - tuesday': [
{
day: 'TUESDAY',
date: '2021-03-09',
timetables: [
{
day: 'TUESDAY',
slots: [
'07:00 - 07:30',
'07:30 - 08:00'
],
changed: false
}
]
}
],
'march 2021 - thursday': [
{
day: 'THURSDAY',
date: '2021-03-18',
timetables: [
{
day: 'THURSDAY',
slots: [
'10:00 - 10:30',
'10:30 - 11:00'
],
changed: false
}
]
}
]
};
/*
this second attempt it might be correct but I'm not sure
*/
{
// Use small functions that do one thing
const timeTableHasChanged = tt => hasChanged = tt.changed === true
const dayhasChanged = day => day.timetables.some(timeTableHasChanged)
const portalHasChanged = pd => {
// Use `values()` not `keys()`. You are not interested in the keys here
const pdv = Object.values(pd)
// early return on empty input, so you don't have too many nested conditionals
if (pdv.length === 0) {
return true; // maybe false. Depends on what you want, if the inpout is empty
}
return pdv.every(item => item.every(dayhasChanged))
}
console.log(portalHasChanged(portalDays)) // should by false
portalDays['march 2021 - monday'][0].timetables[0].changed = true
portalDays['march 2021 - monday'][1].timetables[1].changed = true
portalDays['march 2021 - tuesday'][0].timetables[0].changed = true
portalDays['march 2021 - thursday'][0].timetables[0].changed = true
console.log(portalHasChanged(portalDays)) // should be true
}
Upvotes: 1