Pouyan Ahmadpour
Pouyan Ahmadpour

Reputation: 23

React native json stringify return my array correctly but it cannot show specific object in array

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.

Upvotes: 1

Views: 512

Answers (1)

Paul Rumkin
Paul Rumkin

Reputation: 6883

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.

Upvotes: 1

Related Questions