Home > database >  How to map a JSON response with different indexes
How to map a JSON response with different indexes

Time:05-10

I'm tryin to map a JSON response from API with different indexes in react-native.

This is the JSON RESPONSE stored in state modules and I wanted to get the label value inside of every modules.

{
    "attributes": {
        "Accounts": {
            "label": "Accounts",
            "access": [
                "access",
                "delete",
                "edit",
                "export",
                "import",
                "list",
                "massupdate",
                "view"
            ]
        },
        "AM_ProjectTemplates": {
            "label": "Projects - Templates",
            "access": [
                "access",
                "delete",
                "edit",
                "export",
                "import",
                "list",
                "massupdate",
                "view"
            ]
        }
    }
}

I tried using this code

this.state.modules.map(item=> {
  return item.label
})

CodePudding user response:

If you want an array of label values then:

Object.entries(this.state.modules.attributes).map(([k, v]) => v.label);

CodePudding user response:

You can use Object.values.

Object.values(this.state.modules.attributes);
  • Related