I have the following object
familyData: [
{ man: 1, woman:10, daughter:6, son:3 },
{ man: 4 woman:5, daughter:5, son:2 },
{ man: 1, woman:1, daughter:1, son:1 },
{ man: 1, woman:1, daughter:1, son:1 },
{ man: 1, woman:1, daughter:1, son:1 },
],
What I am trying to do is delete 1 from a each index, however the number of subtractions is user submitted.
let me explain...
If the user inserts a 2. 1 will be taken away from the first 2 indexes in the object
{ man: 0, woman: 9, daughter:6, son:3 },
{ man: 3 woman:4, daughter:5, son:2 },
If the user inserts a 4. 1 will be taken away from the first 4 indexes in the object
{ man: 0, woman: 9, daughter:5, son:2 },
{ man: 3 woman:4, daughter:4, son:1 },
I have tried a variety of for and do-while loops but cannot seem to nail it.
Try:
var indexes = 0;
for (var i=0; i<familyData.length; ++i) {
for (var key in familyData[i]) {
if (++indexes < 4 && familyData[i][key]) {
familyData[i][key]--;
}
}
}