javascript JS 物件陣列 數量加總


            // 資料陣列
            var array = [
                { Name: 'Apple', Price: 300 },
                { Name: 'Orange', Price: 180 }
            ]

            // es2015 計算加總
            var total = array.reduce(function (a, b) {
                return a + b.Price;
            },0);

            // 顯示總和
            console.log("es2015=>" + total);
           
            // es2016 計算加總
            var total2 = array.reduce((a, b) => {
                return a + b.Price;
            },0);
           
            // 顯示總和
            console.log("es2016=>" + total2);

留言

這個網誌中的熱門文章

[java]BIG5 字集判斷

C#WEB API 無法抓取檔名:Refused to get unsafe header “Content-Disposition”