React - 将对象转换为具有属性的对象数组 [英] React - convert object into array of objects with properties
本文介绍了React - 将对象转换为具有属性的对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下对象
"data":{
"name 1":"a",
"name 2":"b",
"name 3":"b",
},
如何转换为同时保留名称和数据a"、b"的对象数组,以便我可以为每个传入名称和数据的组件映射和渲染?
How can I convert to array of objects that will keep both the name and data "a", "b" so I can map and render compoents for each one passing in the both the name and data?
推荐答案
如果您使用 reduce 函数,您可以执行以下操作来实现您的目标
If you use a reduce function you can do the following to achieve your goal
Object.keys(data).reduce((array, key) => {
return [...array, {key: data[key]}]
}, [])
减少
是一个很酷的函数,它迭代并将数据组合成一个项目(可以是 1 个对象、数组、整数等).
Reduce
is a cool function that iterates and combine data into one single item (could be 1 object, array, integer, etc).
Object.keys()
是一种从当前对象中获取每个键并能够遍历每个键的方法.
Object.keys()
is a way to get each key from the current object and be able to iterate over each.
这篇关于React - 将对象转换为具有属性的对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文