今天有一陣列
var full = [
{
month: "2019/10",
ind: "TSWE",
exp: 11,
},
{
month: "2019/10",
ind: "MSCI",
exp: 12,
},
{
month: "2019/11",
ind: "TSWE",
exp: 13,
},
{
month: "2019/11",
ind: "MSCI",
exp: 14,
},
{
month: "2019/12",
ind: "TSWE",
exp: 15,
},
{
month: "2019/12",
ind: "MSCI",
exp: 16,
},
]
我想依照日期把裡面的exp做相加的動作,並儲存在新陣列
請問如何做 謝謝
例如 2019/10 >> 11+12 = 23
map = {}
full.forEach(element => {
if(map[element['month']] === undefined) {
map[element['month']] = 0
}
map[element['month']] += element['exp']
})
console.log(map)
// Object { "2019/10": 23, "2019/11": 27, "2019/12": 31 }
有了 map
這個物件,你應該就能做想要做的事