将对象属性中的数组扩展到对象数组
我如何将以下对象转换
data = {
sp: [ 'A', 'B', 'C' ],
jh: [ '1', '0', 'AB' ],
oa: [ 27493, 9837, 3781 ]
}
为以下对象数组:
new_data = [
{sp: 'A', jh: '1', oa: 27493},
{sp: 'B', jh: '0', oa: 9837},
{sp: 'C', jh: 'AB', oa: 3781}
]
How am I able to convert the following object:
data = {
sp: [ 'A', 'B', 'C' ],
jh: [ '1', '0', 'AB' ],
oa: [ 27493, 9837, 3781 ]
}
into the following array of objects:
new_data = [
{sp: 'A', jh: '1', oa: 27493},
{sp: 'B', jh: '0', oa: 9837},
{sp: 'C', jh: 'AB', oa: 3781}
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设数组的大小相同,
MAP
使用当前索引 <代码>降低键。Assuming the arrays are all the same size,
map
one of them andreduce
the keys using the current index.object.fromentries
用原始对象的键创建一个对象,并且值为原始对象中该键的数个数组中该索引的值。Object.entries
to get all of the key-value pairs in the object.Object.fromEntries
to create an object with the keys from the original object and the value being the value at that index in the array for that key in the original object.