0

I am working with React, Redux and JS with a tutorial which used this dummy object as part of the redux store.

const initState = {
    posts:[
       [
        {id: '1', title: 'Fire', body: 'Squirtle Laid an Egg'},
        {id: '2', title: 'Land', body: 'Charmander Laid an Eg'},
        {id: '3', title: 'More Land', body: 'a Helix Fossil was Found'}
       ]
    ]
}

In the tutorial, the producer uses posts.map() to access the data. Is there a way to access this data without the map function? I tried

<p>{posts[0].id}</p> 

but I get the following error:

Uncaught TypeError: Cannot read property '0' of undefined

What would be the proper notation to only get the first id value of this object?

2 Answers2

1

You are missing the wrapping object, and you want to access the first element of posts initState.posts[0][0].id

CygnusOlor
  • 94
  • 4
0

Note that posts[0] is an array with 3 objects inside of it. It's nested, so you'd have to call posts[0][0].id

Adam Redmond
  • 87
  • 1
  • 4