Home > database >  How to map Data data(Array) ,if data was a value of object
How to map Data data(Array) ,if data was a value of object

Time:10-28

const [data,setData]=useState([])

i have Received that data as {results:[20objects],info:{}}

i have tried console log of Object.values(data) showing that [[20 objects],{}]

i have to do map over that 20 objects Array ?

CodePudding user response:

From your logging [[20 objects],{}] it seems that your data object is an array and your results array is the 1st element. So you can access it through data[0].

Then you can map over these 20 objects by using .map on your array.

The code should look like this:

data[0].map((object) => { //some logic here })

CodePudding user response:

Assuming data is {results:[20objects],info:{}}:

data.results.map((item) => {
    // do stuff here
})

or

data['results'].map((item) => {
    // do stuff here
})
  • Related