gpt4 book ai didi

javascript - 如何从数组对象中的嵌套数组中获取数据

转载 作者:行者123 更新时间:2023-11-30 14:10:24 24 4
gpt4 key购买 nike

我有一个这样的对象数组

array = {
"microservices": [
{
"id": 1,
"permissions": [
{
"id": 65,
"name": "Default permission"
},
{
"id": 64,
"name": "Super user Access"
}
]
},
{
"id": 2,
"permissions": []
},
{
"id": 3,
"permissions": [
{
"id": 18,
"name": "Script: Update"
},
{
"id": 15,
"name": "Application: Delete"
},
{
"id": 9,
"name": "Workspace: Create"
}
]
}
]
}

从微服务数组我只想获取“权限”数组并存储到一个新变量,我搜索了它但没有得到任何相关解决方案,在此先感谢

最佳答案

您可以使用 map 运算符并单独获取该特定属性。请检查以下 JS 示例。

const array = {
"microservices": [{
"id": 1,
"permissions": [{
"id": 65,
"name": "Default permission"
},
{
"id": 64,
"name": "Super user Access"
}
]
},
{
"id": 2,
"permissions": []
},
{
"id": 3,
"permissions": [{
"id": 18,
"name": "Script: Update"
},
{
"id": 15,
"name": "Application: Delete"
},
{
"id": 9,
"name": "Workspace: Create"
}
]
}
]
}
const output = []
array.microservices.map(function(x) {
output.push(...x.permissions);
})
console.log(output);

关于javascript - 如何从数组对象中的嵌套数组中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54570105/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com