js如何将二维对象数组生成一维对象数据
[
{
name: 'A组',
data:[
{date: '2019-08-31', count: 988},
{date: '2019-09-30', count: 823},
{date: '2019-10-31', count: 235},
{date: '2019-11-30', count: 1042},
{date: '2019-12-31', count: 988},
{date: '2020-01-31', count: 823},
{date: '2020-02-29', count: 235},
{date: '2020-03-31', count: 1042},
{date: '2020-04-30', count: 988},
{date: '2020-05-31', count: 823},
{date: '2020-06-30', count: 235},
{date: '2020-07-28', count: 1042}
]
},
{
name: 'B组',
data:[
{date: '2019-08-31', count: 988},
{date: '2019-09-30', count: 823},
{date: '2019-10-31', count: 235},
{date: '2019-11-30', count: 1042},
{date: '2019-12-31', count: 988},
{date: '2020-01-31', count: 823},
{date: '2020-02-29', count: 235},
{date: '2020-03-31', count: 1042},
{date: '2020-04-30', count: 988},
{date: '2020-05-31', count: 823},
{date: '2020-06-30', count: 235},
{date: '2020-07-28', count: 1042}
]
},
{
name: 'C组',
data:[
{date: '2019-08-31', count: 988},
{date: '2019-09-30', count: 823},
{date: '2019-10-31', count: 235},
{date: '2019-11-30', count: 1042},
{date: '2019-12-31', count: 988},
{date: '2020-01-31', count: 823},
{date: '2020-02-29', count: 235},
{date: '2020-03-31', count: 1042},
{date: '2020-04-30', count: 988},
{date: '2020-05-31', count: 823},
{date: '2020-06-30', count: 235},
{date: '2020-07-28', count: 1042}
]
},
{
name: 'D组',
data:[
{date: '2019-08-31', count: 988},
{date: '2019-09-30', count: 823},
{date: '2019-10-31', count: 235},
{date: '2019-11-30', count: 1042},
{date: '2019-12-31', count: 988},
{date: '2020-01-31', count: 823},
{date: '2020-02-29', count: 235},
{date: '2020-03-31', count: 1042},
{date: '2020-04-30', count: 988},
{date: '2020-05-31', count: 823},
{date: '2020-06-30', count: 235},
{date: '2020-07-28', count: 1042}
]
}
]
上面的数据 怎么转换为这样的一维数组数据
[
{date: '2019-08-31', 'A组': 988, 'B组': 988, 'C组': 988, 'D组': 988},
{date: '2019-09-30', 'A组': 823, 'B组': 823, 'C组': 823, 'D组': 823},
{date: '2019-10-31', 'A组': 235, 'B组': 235, 'C组': 235,'D组': 235},
{date: '2019-11-30', 'A组': 1042, 'B组': 1042, 'C组': 1042, 'D组': 1042},
{date: '2019-12-31', 'A组': 988, 'B组': 988,'C组': 235,'D组': 235},
{date: '2020-01-31', 'A组': 823, 'B组': 235,'C组': 235,'D组': 235},
.
.
.
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题解决
思路分析
看了下需求,就是合并数据后再分组
如果老是这样遍历的话,在数据量大的时候,时间复杂度就老高老高了.
所以得想办法减少遍历的次数
一个简单的方法:
大致思路就是使用一个 Map(就用 Object 也可以) 来中转数据。
Map 的 key 采用 date 的值,然后 value 中存储 A组 B组 等各种字段所构成的一个 Object。
遍历所有的数据,填充到对应的 Map item 中,最后遍历整个 Map,拼接成你想要的这种数据即可。
Map 的大致结构如下: