2 回答
TA贡献1785条经验 获得超4个赞
干得好。
var data = [
{
"date":"2010-01-01",
"name":"a"
},
{
"date":"2010-02-01",
"name":"b"
},
{
"date":"2010-03-01",
"name":"c"
},
{
"date":"2010-01-01",
"name":"aa"
},
]
const resultSet = {};
for (let i = 0, _len = data.length; i < _len; i++ ) {
if(resultSet[data[i].date]){
resultSet[data[i].date] += ","+data[i].name
}
else{
resultSet[data[i].date] = data[i].name
}
}
const resultArr = Object.entries(resultSet).map(([key,value])=>{
return { date: key, name: value }
})
console.log(resultArr)
TA贡献1844条经验 获得超8个赞
尝试:
var data = [
{
"date":"2010-01-01",
"name":"a"
},
{
"date":"2010-02-01",
"name":"b"
},
{
"date":"2010-03-01",
"name":"c"
},
{
"date":"2010-01-01",
"name":"aa"
},
]
var result1 = data.reduce((accum, curr) => {
if (!accum[curr.date]) {
return {...accum, [curr.date]: [curr.name] }
}
return {...accum, [curr.date]: [...accum[curr.date], curr.name] }
}, {})
var final = Object.keys(result1).map(date => ({
date: date,
name: result1[date].join(',')
}))
console.log(final)
添加回答
举报