reactjs - React.js:从状态数组中获取值作为字符串
问题描述
我有这个嵌套对象数组
(2) […]
0: {…}
actions: (3) […]
0: Object { actionId: "C7FCD770-7868-4357-8CA6-57A5C4DA1F1E", action: "ClientDetailsNew", description: "Enroll new Customer for Pesonet" }
1: Object { actionId: "D324327C-6709-4AF0-B2B3-A10557BFA6F4", action: "ClientDetailsDelete", description: "Delete Client details" }
2: Object { actionId: "7DFA8A4C-2C56-4D89-875D-CE522413E81B", action: "ClientDetailsUpdate", description: "Modify Client details" }
length: 3
<prototype>: Array []
description: "Client Enrollment"
module: "Client Maintenance"
moduleId: "4E948025-1F1B-41E2-A18D-55AD8646810B"
<prototype>: Object { … }
1: {…}
actions: (2) […]
0: Object { actionId: "05D7DF97-C794-4648-AAA0-506CAE35125B", action: "BankDetailsNew", description: "Enroll new bank for Pesonet" }
1: Object { actionId: "03ABA996-FB9A-4DA5-BD7C-7EC5E31F4A99", action: "BankDetailsDelete", description: "Delete bank details" }
length: 2
<prototype>: Array []
description: "Bank Enrollment"
module: "Bank Maintenance"
moduleId: "C77C1031-2714-483D-AE59-21C9CD5EBAEF"
<prototype>: Object { … }
length: 2
<prototype>: Array []
我想得到所有的actionId。我尝试了以下解决方案:
const actionsNewMap = profileModule.map((actionsVar) => ({actions: actionsVar.actions}))
const actionIdNewMap = actionsNewMap.map(({actions}) => (actions.map((actionsMapVar) => ({actionId: actionsMapVar.actionId}))))
const actionIdNewMapList = actionIdNewMap.map(actionId => actionId);
到目前为止,我得到了这些值:
(2) […]
0: (3) […]
0: Object { actionId: "C7FCD770-7868-4357-8CA6-57A5C4DA1F1E" }
1: Object { actionId: "D324327C-6709-4AF0-B2B3-A10557BFA6F4" }
2: Object { actionId: "7DFA8A4C-2C56-4D89-875D-CE522413E81B" }
length: 3
<prototype>: Array []
1: (2) […]
0: Object { actionId: "05D7DF97-C794-4648-AAA0-506CAE35125B" }
1: Object { actionId: "03ABA996-FB9A-4DA5-BD7C-7EC5E31F4A99" }
length: 2
<prototype>: Array []
length: 2
<prototype>: Array []
问题是我只想要 1 个字符串数组而不是多个对象数组。
我可以知道我错过了什么吗?地图不足以满足这种要求吗?
--EDIT-- 从响应中添加原始有效 JSON
[
{
"moduleId": "4E948025-1F1B-41E2-A18D-55AD8646810B",
"module": "Client Maintenance",
"description": "Client Enrollment",
"actions": [
{
"actionId": "C7FCD770-7868-4357-8CA6-57A5C4DA1F1E",
"action": "ClientDetailsNew",
"description": "Enroll new Customer for Pesonet"
},
{
"actionId": "D324327C-6709-4AF0-B2B3-A10557BFA6F4",
"action": "ClientDetailsDelete",
"description": "Delete Client details"
}
]
}
]
我正在尝试将所有actionId
内容作为数组字符串
解决方案
考虑到您的数组名称是 profileModule:
profileModule.map(item => item.actions.map(action => action.actionId)).flat()
让我知道这是否有效,或者您找到了更好的解决方案。
编辑:感谢这个 SO 答案。从这里了解平面功能。
推荐阅读
- java - 无法将json字符串绑定到java字符串
- python - json 库将空格字符解释为 "\xa0"
- r - 在 R 中分配自定义周数
- python - 在 cmd 中安装 CX_ORACLE 时出错
- sql - 使用 WHERE 限制返回的记录?
- django - 在 Windows 上使用 Django 的 Pyinstaller - “环境只能包含字符串”
- ios - 以编程方式在 UITabBarController 和 UINavigationController 中呈现视图
- python - 有没有更好的方法来删除给定子目录中的所有 .py 和 .pyc 文件?
- google-cloud-platform - 从谷歌云中的虚拟机锁定
- javascript - JavaScript 检查值是否优越并尊重倍数