假如有这样一组数据
var oldData=[
{date:'2019-08-26',"queue_id":219,"queue_num":4,"queue_status":1,"queue_name":'预约接种'},
{date:'2019-08-26',"queue_id":218,"queue_num":2,"queue_status":1,"queue_name":'预约接种'},
{date:'2019-08-25',"queue_id":217,"queue_num":1,"queue_status":1,"queue_name":'预约接种'}
]
我想把拥有相同日期的数据,放在一个数组里面,
第一种方案
1、首先我们把源数据先变成目标数据的规则
var oldDataRule = []
oldData.forEach(el => {
var oldObj = {
date: el.date,
list:[]
}
oldObj.list.push(el)
oldDataRule.push(oldObj)
})
console.log(oldDataRule)
打印出来的数据如下
2、然后我们把源数据去重,把相同日期的数据合并成list
var newData = []
var newObj = {}
oldDataRule.forEach((el, i) => {
if (!newObj[el.date]) {
newData.push(el);
newObj[el.date] = true;
} else {
newData.forEach(el => {
if (el.date === oldDataRule[i].date) {
el.list = el.list.concat(oldDataRule[i].list);
// el.list = [...el.list, ...oldDataRule[i].list]; // es6语法
}
})
}
})
console.log(newData)
最后出来的结构如下
QQ截图20190830150433.png第二种方案
var oldDataRule = []
var newData = []
oldData.forEach( item =>{
oldDataRule [item.date] = oldDataRule [item.date] || [];
oldDataRule [item.date].push(item);
})
for (let key in oldDataRule ) {
let obj = {}
obj.date = key
obj.list =arr[key]
newData.push(obj)
}
网友评论