![](/img/trans.png)
[英]how to extract all objects from nested arrays into one array in React
[英]Extract property values from nested array of objects and arrays
我有以下嵌套的对象数组:
[
{
"info": [
{
"period": {
"start": "2020-01-01",
"end": "2020-01-31"
},
"info": [
{
"id": 036,
"name": "john",
},
{
"id": 037,
"name": "inna",
}
]
}
]
},
{
"info": [
{
"period": {
"start": "2020-01-01",
"end": "2020-01-31"
},
"info": [
{
"id": 045,
"name": "carl",
},
{
"id": 056,
"name": "tina",
}
]
}
]
}]
我想提取“name”属性的所有值并将它们放入一个数组中。
输出: ["john", "inna", "carl", "tina"]
。
尝试使用此代码:
const userNames = [];
data.map(item => {
return item.info.map(registry => {
return registry.info.map(user => userNames.push(user.name));
})
})
输出: ["john", "inna", "carl", "tina"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.