Here are two ways to do it (modern & shorter vs. backwards-compatible & longer):
var data = {
mo: true,
tu: true,
we: true,
th: true,
fr: true,
sa: false,
su: false
};
const short = Object.values(data).filter((v) => v).length;
var long = 0;
for (var prop in data) {
if (data.hasOwnProperty(prop) && data[prop]) {
long++;
}
}
console.log(short, long);
In the modern version, we take only the values in the object, filter out any that aren’t truthy, and take the length of the resulting array.
In the backwards-compatible version, we loop through the properties of the object, filter out any that aren’t its own properties or aren’t truthy, and increment the count.