Reputation: 23
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
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