This question already has answers here:
How do I loop through or enumerate a JavaScript object?
(41个回答)
2年前关闭。
嗨,我想知道如何使用for循环将所有值加到一个对象中
(41个回答)
2年前关闭。
嗨,我想知道如何使用for循环将所有值加到一个对象中
var myBills = {
myMumAndDad: 1000,
myFamily: 2500,
myPhone: 550,
myTrain: 650,
myTraining: 200,
myInternet: 200,
myAdobe: 200,
myElectric: 400,
myWeb: 100,
myEditor: 100,
myRent: 3000,
myFood: 2000,
};
// Få lavet et loop til at + disse sammen
var allBills = myBills.myMumAndDad + myBills.myFamily + myBills.myPhone + myBills.myTrain + myBills.myTraining + myBills.myInternet + myBills.myAdobe + myBills.myElectric + myBills.myEditor + myBills.myWeb + myBills.myRent + myBills.myFood;
最佳答案
var sum = 0;
for(var key in myBills){
sum += myBills[key];
}
07-25 23:13