I have this array dataset that I copied from chrome console that was produced by this code
var cartarry = JSON.parse(localStorage.getItem("cart"));
console.log(cartarry)
products
:
Array(5)
{id: "1510-01-312-3501-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514542566148", name: "AIRPLANE UTILITY", price: "$90", quantity: "1"}
1
:
{id: "1510-01-312-3501-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514542566148", name: "AIRPLANE UTILITY", price: "$90", quantity: "1"}
2
:
{id: "1510-00-033-6312-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514540733034", name: "AIRPLANE UTILITY", price: "$43", quantity: "3"}
3
:
{id: "1510-00-033-6312-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514540733034", name: "AIRPLANE UTILITY", price: "$43", quantity: "1"}
4
:
{id: "1510-00-033-6312-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514540733034", name: "AIRPLANE UTILITY", price: "$43", quantity: "1"}
length
:
5
I want to loop through the array and get each of the id, name, price and quantity. I tried using this this code
cartarry.forEach(function(element) {
console.log(element);
})
but I got this error
cartarry.forEach is not a function
i tried retrieving each manual with this
cartarry[1]
but nothing happens. What am I doing wrong and how can I loop the array of products to obtain the property of each product?
It looks like cartarry.products
might hold the array. Try cartarry.products.forEach()
.
I assume you have a data structure like this:
{
'products': [
{id: "1510-01-312-3501-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514542566148", name: "AIRPLANE UTILITY", price: "$90", quantity: "1"},
...
]
}
So in order to get to the ids you need to:
cartarry.products.forEach();
Go step by step, check what cartarry.products
type is, it should be an Array.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.