繁体   English   中英

React.js:从 state 数组中获取值作为字符串

[英]React.js: Get values as string from a state array

我有这个嵌套对象数组

(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 个字符串数组而不是多个对象数组。

我可以知道我错过了什么吗? map 不足以满足这种要求吗?

--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 答案 从这里找到关于平面 function 的信息。

你需要的是:

const actionIds = data.reduce((acc, subData) => 
    acc.concat(subData.actions.map(action => action.actionId)), [])

 const data = [{ "moduleId": "4E948025-1F1B-41E2-A18D-55AD8646810B", "module": "Client Maintenance", "description": "Client Enrollment", "actions": [{ "actionId": "1-C7FCD770-7868-4357-8CA6-57A5C4DA1F1E", "action": "ClientDetailsNew", "description": "Enroll new Customer for Pesonet" }, { "actionId": "1-D324327C-6709-4AF0-B2B3-A10557BFA6F4", "action": "ClientDetailsDelete", "description": "Delete Client details" } ] }, { "moduleId": "4E948025-1F1B-41E2-A18D-55AD8646810B", "module": "Client Maintenance", "description": "Client Enrollment", "actions": [{ "actionId": "2-C7FCD770-7868-4357-8CA6-57A5C4DA1F1E", "action": "ClientDetailsNew", "description": "Enroll new Customer for Pesonet" }, { "actionId": "2-D324327C-6709-4AF0-B2B3-A10557BFA6F4", "action": "ClientDetailsDelete", "description": "Delete Client details" } ] }] const actionIds = data.reduce((acc, subData) => acc.concat(subData.actions.map(action => action.actionId)), []) console.info(actionIds)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM