js根据某字段对数组对象进行分组

问题
希望能把当前的数组,根据相同的【日期】,放在一个数组里面;

let sourceData=https://www.it610.com/article/[ {date:"2021-01-01",name:"LuckyJin"}, {date:"2021-01-01",name:"JinFed"}, {date:"2021-02-01",name:"olivivian"} ]

var dataTemp = [] var newData = https://www.it610.com/article/[] sourceData.forEach( item =>{ dataTemp[item.date] = dataTemp[item.date] || []; dataTemp[item.date].push(item); }) console.log('格式一',dataTemp)

效果:

js根据某字段对数组对象进行分组
文章图片
image.png
for (let key in dataTemp ) { let obj = {} obj.date = key obj.list = dataTemp[key] newData.push(obj) } console.log('格式二',newData);

【js根据某字段对数组对象进行分组】效果:

js根据某字段对数组对象进行分组
文章图片
image.png

    推荐阅读