Dawn Seeker
Dawn Seeker

Reputation: 37

How to map a JSON response with different indexes

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
})

Upvotes: 0

Views: 94

Answers (2)

Abe
Abe

Reputation: 5508

You can use Object.values.

Object.values(this.state.modules.attributes).map(a => a.label);

Upvotes: 1

AE0011
AE0011

Reputation: 199

If you want an array of label values then:

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

Upvotes: 1

Related Questions