2 回答
TA贡献1840条经验 获得超5个赞
您需要使用 。reduce()
var items = [ {title: 'Hiking', date: new Date('2020-05-10')}, {title: 'Shopping', date: new Date('2020-05-10')},{title: 'Hiking', date: new Date('2020-05-22')}, {title: 'Football', date: new Date('2020-05-22')}, {title: 'Football', date: new Date('2020-05-28')}, {title: 'Hiking', date: new Date('2020-05-28')}, {title: 'Cricket', date: new Date('2020-05-28')}];
var result = items.reduce((acc, {title, date})=>{
dateKey = new Date(date).toISOString().slice(0,10);
acc[dateKey] = acc[dateKey] || [];
acc[dateKey].push({title});
return acc;
},{});
console.log(result);
TA贡献1998条经验 获得超6个赞
试试这个
let items = [
{title: 'Hiking', date: new Date('2020-05-10')},
{title: 'Shopping', date: new Date('2020-05-10')},
{title: 'Hiking', date: new Date('2020-05-22')},
{title: 'Football', date: new Date('2019-05-22')},
{title: 'Football', date: new Date('2020-05-28')},
{title: 'Hiking', date: new Date('2020-05-28')},
{title: 'Cricket', date: new Date('2020-05-28')},
];
items.sort(function(a,b){
return a.date - b.date;
});
console.log(items);
添加回答
举报