(excluding foreach, map, reduce, filter, for, while and do while) (return true (if no object with attribute read : false is found) or false (if any one of the objects contains property read : false).) Imagine you have the following array:
let allRead = true;
let notifications = [
{message: ‘Lorem’, read: true},
{message: ‘Ipsum’, read: true},
{message: ‘Dolor’, read: true},
{message: ‘Sit’, read: false},
{message: ‘Amet’, read: true}
];
You need to change the allRead variable to false using a built-in higher-order function on the notifications array. Rules: a) You cannot utilize for, while, do-while loops b) You are not allowed to use forEach(), map(), reduce(), filter().
I've tried using some and find. It seems like find always returns the complete object, so it might not be suitable in this case.
allRead = notifications.find((obj) => {
console.log("yes");
if (obj.read === false) {
console.log(obj.read);
return obj;
}
});
console.log(allRead);
On the other hand, utilizing some has been somewhat successful...but it only returns true when read : false is found whereas I want to set allRead to false whenever read: false is encountered, regardless of other iterations.
allRead = notifications.some((not) => not.read !== true);
console.log(allRead);
I also observed that using if else statements or switch cases and returning true/false based on conditions automatically breaks out of the loop once a certain condition is met.
allRead = notifications.some((not) => {
switch (not.read) {
case false:
break;
return false;
default:
return true;
}
});
console.log(allRead);