2 回答
TA贡献1811条经验 获得超5个赞
const week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
const weekdata = week.map((day, idx) => newData.reduce((item, v) => item + parseFloat(v[day]), 0)
后面那块逻辑是按照你代码搬过来了,出问题不怪我 :)
TA贡献1809条经验 获得超8个赞
安利一下柯里化:
const getTotal = function (list) {
return function (day) {
return list.reduce((result, item) => (
result + parseFloat(item[day])
), 0);
};
}
const getWeekdayTotal = getTotal(newData);
// single day
let mondayTotal = getWeekdayTotal("Monday");
let tuesdayTotal = getWeekdayTotal("Tuesday");
// multiple days
let days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
let daysTotal = days.map(getWeekdayTotal);
添加回答
举报