I have a big object like this
var bigObj = {
'main-dish': {
'lasagna': true,
'pasta': false,
},
'others': {
'salad': true,
'soup': true,
},
}
and a small one, that should count how many ‘false’ values are for each key in the bigObj
by default all is 0
var smallObj = {
'main-dish': 0,
'others': 0
}
I built a UI in Vue.js that updates the booleans in real time
and with every change I check for the true/false with this for loop
The problem: if I update the booleans from the bottom or middle the smallObj is not updated;
Only if I start from the first key: lasagna the smallObj is being updated;
for(var i = 0; i < Object.keys(bigObj).length; i ++) {
if(Object.keys(smallObj)[i] === Object.keys(bigObj)[i]) {
for(var j =0; j < Object.keys(bigObj[Object.keys(bigObj)[i]]).length; j++) {
if(!Object.values(bigObj[Object.keys(bigObj)[i]])[j]) {
smallObj[Object.keys(bigObj)[i]]++
console.log(Object.keys(bigObj[Object.keys(bigObj)[i]])[j]);
} else {
console.log(Object.keys(bigObj[Object.keys(bigObj)[i]])[j]);
}
}
}
}
console.log(smallObj)
// {main-dish: 1, others: 0}
Is the for loop the right approach? How can I create the smallObj is it’s updated regardless what value I update first?