I have a scenario where I need to update the 'checked' key value to true for objects in an array based on another array's values.
var daysActive = ['monday', 'tuesday', 'wednesday'];
var weekDays = [{ "name": "Sunday", "value": "sunday", "id": "SUN","checked":false,disabled:true },
{ "name": "Monday", "value": "monday", "id": "MON" ,"checked":false,disabled:true},
{ "name": "Tuesday", "value": "tuesday", "id": "TUE","checked":false,disabled:true },
{ "name": "Wednesday", "value": "wednesday", "id": "WED","checked":false,disabled:true },
{ "name": "Thursday", "value": "thursday", "id": "THU","checked":false,disabled:true },
{ "name": "Friday", "value": "friday", "id": "FRI","checked":false,disabled:true },
{ "name": "Saturday", "value": "saturday", "id": "SAT","checked":false,disabled:true}
];
The final array should have its 'checked' key values updated as follows:
var resultantDays = [{ "name": "Sunday", "value": "sunday", "id": "SUN","checked":false, disabled:true },
{ "name": "Monday", "value": "monday", "id": "MON" ,"checked":true, disabled:true},
{ "name": "Tuesday", "value": "tuesday", "id": "TUE","checked":true, disabled:true },
{ "name": "Wednesday", "value": "wednesday", "id": "WED","checked":true, disabled:true },
{ "name": "Thursday", "value": "thursday", "id": "THU","checked":false, disabled:true },
{ "name": "Friday", "value": "friday", "id": "FRI","checked":false, disabled:true },
{ "name": "Saturday", "value": "saturday", "id": "SAT","checked":false, disabled:true}
];
I have attempted a solution, but the approach seems to be incorrect.
var modifiedWeekdays = daysActive.map(day => {
weekDays.forEach(function(val) {
if (val.value === day) {
val.checked = true;
}
});
});