I have the following array given:
const myArray = [{ id: 1, isSet: true }, { id: 2, isSet: false }, ...];
Actually I want to iterate only the isSet properties of the objects (not all properties). The most simple solution, which came on my mind was the following:
let isSet = false;
for (const obj of myArray) {
for (const property in obj) {
if (property === "isSet" && obj.hasOwnProperty(property)) {
isSet |= obj[property];
}
}
}
console.log(isSet);
I think this one does not really look pretty, so has someone a better solution as the given one (maybe also better in runtime)?
Thanks in advance!
allAreSet = myArray.every(x => x.isSet)- this?isSet |= obj.isSetor similar? There's no need to iterate over all the properties in each object in the array..some()instead of.every(). Based on the above code, I believe OP only cares if any of them are set, not if all of them are set