Answer the question
In order to leave comments, you need to log in
How to output json content by key?
Goodnight. Please tell me how can I correctly display the JSON content based on the key that is stored in LocalStorage.
The API returns me this kind of JSON
let goods = [
{
"id":"1",
"name":"Pizza",
"cost":3
},
{
"id":"2",
"name":"Cola",
"cost":5
},
{
"id":"3",
"name":"Tomatoes",
"cost":6
}]
{1: 3, 2: 2, 3: 1}
function addToCart(id) {
if (cart[id]!=undefined) {
cart[id]++;
}
else {
cart[id] = 1;
}
localStorage.setItem('cart', JSON.stringify(cart) );
}
function getCartFromLS() {
return JSON.parse(localStorage.getItem('cart'));
}
let cart = getCartFromLS();
for(let key in cart){
console.log(goods[key].name);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question