1

When I use {alert(JSON.stringify(cart[0]))} in my react-native app it shows me the whole array of object like this:

[{
  "id": 3,
  "name": John,
  .
  .
}]

but when I use {alert(JSON.stringify(cart[0].id))} for example it returns me undefined.

0

1 Answer 1

1

The code you provided contains object with two nested arrays:

const cart = [
   [
     {
       id: 3,
       name: 'John',
       // ...
     }
   ],
]

When you try to receive value of cart[0].id you actually refer to id property of the second array. To extract the value you want use cart[0][0].id.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks alot....but how can i refer this items inot FlatList data?? i used to use const { cart } = this.props
According to the rules of StackOverflow you should to create new question and describe this exact problem (with FlatList) to let others answer your question. Thank you.
Also, please mark correct answers as correct (if they are) using a checkmark. stackoverflow.com/help/accepted-answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.