var arr = [ { name: '节点1', value: 1, type: 2, date: '2018-06-07T08:00:01.589Z' }, { name: '组件T1', value: 1, type: 2, date: '2018-06-07T08:10:01.589Z' }, { name: '节点2', value: 1, type: 4, date: '2018-06-07T20:00:01.589Z' }, { name: '节点2', value: 4, type: 4, date: '2018-06-07T20:10:21.189Z' }, { name: '节点4', value: 1, type: 4, date: '2018-06-07T08:00:01.560Z' }, { name: '节点4', value: 2, type: 4, date: '2018-06-07T08:10:31.584Z' }, { name: '节点6', value: 1, type: 3, date: '2018-06-07T08:00:01.589Z' }, { name: '节点5s', value: 1, type: 4, date: '2018-06-07T08:00:01.589Z' }, { name: '组件T2', value: 1, type: 4, date: '2018-06-07T08:00:01.589Z' }, { name: '组件T1', value: 4, type: 4, date: '2018-06-07T08:06:01.589Z' }, { name: '样式5', value: 1, type: 4, date: '2018-06-07T08:00:01.589Z' }, { name: '样式2', value: 5, type: 4, date: '2018-06-02T08:07:01.589Z' }, { name: '样式2', value: 6, type: 4, date: '2018-06-07T08:00:01.589Z' }, { name: '样式3', value: 1, type: 4, date: '2018-06-05T08:00:01.589Z' }, { name: '样式', value: 1, type: 4, date: '2018-06-07T08:00:01.589Z' }, { name: 'css9', value: 7, type: 4, date: '2018-06-04T08:04:01.588Z' }, { name: 'js9', value: 1, type: 4, date: '2018-06-02T08:00:01.577Z' }, { name: 'js9', value: 2, type: 4, date: '2018-06-07T08:00:01.110Z' }, { name: 'js10', value: 1, type: 1, date: '2018-06-07T08:00:01.534Z' } ]; // * 请用您认为最优化的方式,将arr中的type为4的数据过滤出来, // * 然后按相同的 name + date(按天)合并value(value累加), // * 然后按 value 降序(从大到小)排序, // * 最后每行按照 "${name},${本地日期},售出${sum(value)}部" 的格式,如:"节点2,2017年06月08日,售出5部", 打印(console.log)出来。 const settleEvents = (arr) => { const typeArr = arr.filter(v => v.type === 4) var i = 0 let res = [] while (i < typeArr.length - 1) { ++i; const idx = typeArr[i].name + '' + typeArr[i].date for (let j = 0; j < typeArr.length; j++) { const idj = typeArr[j].name + '' + typeArr[j].date let date = new Date(typeArr[j].date) let formatDate = date.getFullYear() + '年' + (date.getMonth() + 1) + '月' + date.getDate() + '日' if (idx === idj) { res.push({ name: typeArr[j].name, date: formatDate, value: typeArr[j].value + typeArr[i].value }) } } } res.sort((a, b) => b.value - a.value).forEach(d => { console.log(`${d.name},${d.date},售出${d.value}部`) }) } settleEvents(arr)