为了账号安全,请及时绑定邮箱和手机立即绑定

js合并相同值的数组,生成一组新的数组

js合并相同值的数组,生成一组新的数组

紫衣仙女 2019-05-22 20:13:26
得到的数据:vardata=[{"year":"2018-03-03","qidong":"17:01:37","shicha":"25","jieshu":"17:02:03"},{"year":"2018-03-03","qidong":"17:01:06","shicha":"14","jieshu":"17:01:20"},{"year":"2018-03-04","qidong":"17:00:41","shicha":"11","jieshu":"17:00:52"},{"year":"2018-03-04","qidong":"17:00:06","shicha":"7","jieshu":"17:00:13"}];请教如何把数组生成新的类型数组?如下vardata=[{"year":"2018-03-03","qidong":"17:01:37,17:01:06","shicha":"25,14","jieshu":"17:02:03,17:01:20"},{"year":"2018-03-04","qidong":"17:00:41,17:00:06","shicha":"11,7","jieshu":"17:00:52,17:00:13"},];
查看完整描述

2 回答

?
翻过高山走不出你

TA贡献1875条经验 获得超3个赞

vardata=[
{"year":"2018-03-03","qidong":"17:01:37","shicha":"25","jieshu":"17:02:03"},
{"year":"2018-03-03","qidong":"17:01:06","shicha":"14","jieshu":"17:01:20"},
{"year":"2018-03-04","qidong":"17:00:41","shicha":"11","jieshu":"17:00:52"},
{"year":"2018-03-04","qidong":"17:00:06","shicha":"7","jieshu":"17:00:13"}
];
var_=data.reduce(function(a,b){
varyear=b.year;
if(!a[year]){
a[year]={
qidong:[],
shicha:[],
jieshu:[]
}
}
a[year].qidong.push(b.qidong);
a[year].shicha.push(b.shicha);
a[year].jieshu.push(b.jieshu);
returna;
},{});
_=Object.keys(_).map(function(item){
return{
year:item,
qidong:_[item].qidong.join(','),
shicha:_[item].shicha.join(','),
jieshu:_[item].jieshu.join(','),
};
})
console.log(_);
                            
查看完整回答
反对 回复 2019-05-22
  • 2 回答
  • 0 关注
  • 704 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信