Using Flat List with objectMap

2.9k Views Asked by At

Im using normalizr to normalize the api response based on id. So the final normalized response will be

{
  "names": {
    "12": {
      "Name": "ABC",
      "Status": "0",
      "Count": 11,
    },
    "31": {
      "Name": "CDE",
      "Status": "1",
      "Count": 12,
    }
  }
}

How i can use this names object map in a flat list. Its not an array but an object map.
What's the best way to do this?

PS: I know there is lodash _.values() function, but i think it will slow for a huge response.

1

There are 1 best solutions below

0
On

First you can change your object to return an array with Object.keys

This your data

{
  "names": {
    "12": {
      "Name": "ABC",
      "Status": "0",
      "Count": 11,
    },
    "31": {
      "Name": "CDE",
      "Status": "1",
      "Count": 12,
    }
  }
}

you can do with

const dataname = data.names
const datanamearr = Object.keys(dataname).map(key => dataname[key])

this will return an array

then you can render your data to FlatList with a map function

{datanamearr.map((item) =>
    <FlatList
      data={item}
      renderItem={({item}) => <Text>{item.Name}</Text>}
    />
  )
}