var shops = [{
captain: "Tim",
chicken: 150,
coke: 200,
fries: 70,
set: 99,
},
{
captain: "Min",
chicken: 99,
coke: 77,
fries: 55,
set:101,
},
{
captain: "Mike",
chicken: 105,
coke: 111,
fries: 75,
set: 130,
},
];
//陣列資料
var shopsTotal = shops.length; //先抓取店面的資料
console.log(shopsTotal); //結果會得到 3
var cokeTotal = 0; //先預設數量為 0
for (var i = 0 ; i < shopsTotal ; i++) { //從第一個開始迴圈;在店家數以內條件以內;加總
cokeTotal+= shops[i].coke; //"+=" 要寫在一起,不能空白
};
console.log("今年的可樂銷售額為: " + cokeTotal); //結果會得到 今年的可樂銷售額為: 388
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.