Javascript 如何转换这两个数据结构?
我怎么能 转换:Javascript 如何转换这两个数据结构?,javascript,arrays,object,Javascript,Arrays,Object,我怎么能 转换: [{"year":2019, "title": "title0"}, {"year":2019, "title": "title1"},{"year":2018, "title": "title2"}, {"year":2018, "title": "title3"}] 致: 使用Javascript?您可以结合使用: const data=[{“年”:2019,“标题”:“标题0”},{“年”:2019,“标题”:“标题1”},{“年”:2018,“标题”:“标题2”},
[{"year":2019, "title": "title0"}, {"year":2019, "title": "title1"},{"year":2018, "title": "title2"}, {"year":2018, "title": "title3"}]
致:
使用Javascript?您可以结合使用:
const data=[{“年”:2019,“标题”:“标题0”},{“年”:2019,“标题”:“标题1”},{“年”:2018,“标题”:“标题2”},{“年”:2018,“标题”:“标题3”}];
const result=Object.values(data.reduce((a,{year,title})=>{
a[年]=a[年]|{年,清单:[]};
a[年份].list.push(标题);
返回a;
}, {}));
控制台日志(结果)代码>
[{"year":2019, "list":["title0", "title1"]}, {"year":2018, "list":["title2", "title3"]}]