5 回答
TA贡献1813条经验 获得超2个赞
var salary = [
{
"postDate": "2017-09-29 00:00:00",
"amountMoney": "495.0"
},
{
"postDate": "2017-09-13 00:00:00",
"amountMoney": "495.0"
},
{
"postDate": "2017-09-04 00:00:00",
"amountMoney": "2526.06"
},
{
"postDate": "2017-08-08 00:00:00",
"amountMoney": "495.0"
},
{
"postDate": "2017-08-08 00:00:00",
"amountMoney": "2526.06"
},
{
"postDate": "2017-07-24 00:00:00",
"amountMoney": "495.0"
},
{
"postDate": "2017-07-19 00:00:00",
"amountMoney": "2526.06"
},
{
"postDate": "2017-06-16 00:00:00",
"amountMoney": "2772.06"
}
];
var rs = [];
var json = {};
for (let i = 0, len = salary.length; i < len; i++) {
var month = salary[i].postDate.split('-')[1];
if (json[month] !== 1) {
rs.push ({
month: month,
amountMoney: Number(salary[i].amountMoney)
})
json[month] = 1;
} else {
for (let j = 0, l = rs.length; j < l; j++) {
if (rs[j].month == month) {
rs[j].amountMoney += Number(salary[i].amountMoney)
}
}
}
}
console.log(rs);
添加回答
举报