const elements = [{praticien: "place_1509136116761", H0709: false, H0911: false, H1113: false, H1315: false}, {praticien: "place_1509136116773", H0709: false, H0911: false, H1113: false, H1315: false}, {praticien: "place_1509136116699", H0709: true, H0911: false, H1113: false, H1315: false}, {praticien: "place_1509136116734", H0709: false, H0911: true, H1113: true, H1315: false}]
const result = {
trueValues: 0,
falseValues: 0
}
elements.forEach(function(element){
for (property in element ){
if (element.hasOwnProperty(property)) {
if (element[property]) result.trueValues++;
else result.falseValues++;
}
}
});
console.log(result);
This function calculates the number of true and false values in all objects. It now checks if the property name starts with 'H'. If you need to include this condition, you can add it inside the
if (element.hasOwnProperty(property))
block.
Take a look at the updated demo here: fiddle
EDIT: Only properties starting with 'H' are considered.
Line modified in the code above:
if (element.hasOwnProperty(property) && property[0] === 'H') {
You can find the new demo at:
new fiddle
NOTE: The console.log
statement was added to indicate which properties are taken into account. In production, remember to comment out or remove it.