Using the array of objects structure below, I need a means of scanning all object elements within this array, i.e. Monday through to Sunday and where the following criteria is met, reset that weeklyFlag to false, i.e.:
weeklyFlag is true
weeklyStartTime is null
weeklyEndTime is null
then reset weeklyFlag to false
So using the above criteria for below example, anywhere where the above criteria is met, I need to reset that specific array object's weeklyFlag to false.
So this would mean for Monday, Tuesday's startEnd[1] index and Saturday.
let weeklyTimes = [
{
"day": "Monday",
"startEnd": [
{
"weeklyFlag": true,
"weeklyStartTime": null,
"weeklyEndTime": null
}
]
},
{
"day": "Tuesday",
"startEnd": [
{
"weeklyFlag": true,
"weeklyStartTime": "2021-08-08T14:00:00.000Z",
"weeklyEndTime": "2021-08-08T15:00:00.000Z"
},
{
"weeklyFlag": true,
"weeklyStartTime": null,
"weeklyEndTime": null
}
]
},
{
"day": "Wednesday",
"startEnd": [
{
"weeklyFlag": false,
"weeklyStartTime": null,
"weeklyEndTime": null
}
]
},
{
"day": "Thursday",
"startEnd": [
{
"weeklyFlag": true,
"weeklyStartTime": "2021-08-08T14:00:00.000Z",
"weeklyEndTime": "2021-08-08T15:00:00.000Z"
},
{
"weeklyFlag": true,
"weeklyStartTime": "2021-08-08T14:00:00.000Z",
"weeklyEndTime": "2021-08-08T15:00:00.000Z"
}
]
},
{
"day": "Friday",
"startEnd": [
{
"weeklyFlag": false,
"weeklyStartTime": null,
"weeklyEndTime": null
}
]
},
{
"day": "Saturday",
"startEnd": [
{
"weeklyFlag": true,
"weeklyStartTime": null,
"weeklyEndTime": null
}
]
},
{
"day": "Sunday",
"startEnd": [
{
"weeklyFlag": false,
"weeklyStartTime": null,
"weeklyEndTime": null
}
]
}
];
I was looking at nested for loops but unsure if array map or filter can be used?
weeklyFlagthen a for-loop would be the best choice, since you modify the existing array instead of creating a new one (e.g. using map)