Solution 1 :

function displayCart()
{
  const items = localStorage.getItem('itemsArray');

  if (items) {
   const parsedItems = JSON.parse(items);
   // Do Stuff With parsedItems 
  }
}

Solution 2 :

You need to get the data from localstorage using the key that you used to save, in this case ‘itemsArray’

You’ve already used the getItem method to get the values

Use the same again as follows :

let returnedValues = JSON.parse(localStorage.getItem('itemsArray'));

Problem :

I want to display the data I have stored in localStorage.

I want to display array of objects that I stored in localStorage of “My Cart” HTML page, where it will display that data when the “Add to Cart” button is clicked.

function addToCartClicked(event)
{
    var button = event.target
    var shopItem = button.parentElement.parentElement
    //var title = shopItem.getElementsByClassName('shop-item-title')[0].innerText
    //var price = shopItem.getElementsByClassName('shop-item-price')[0].innerText
    //var imgSrc = shopItem.getElementsByClassName('shop-item-image')[0].src

    var oldItems = JSON.parse(localStorage.getItem('itemsArray')) || [];

    var newItem = 
    {
    'product-name': shopItem.getElementsByClassName('shop-item-title')[0].innerText,
    'product-image': shopItem.getElementsByClassName('shop-item-image')[0].src,
    'product-price': shopItem.getElementsByClassName('shop-item-price')[0].innerText
    };

 oldItems.push(newItem);

 localStorage.setItem('itemsArray', JSON.stringify(oldItems));

}


function displayCart()
{

}

Comments

Comment posted by dipser

There is everything you need in your question. Just call displayCart(); from “addToCartClicked(event)”.

Comment posted by M A Salman

Dont post solution directly.Give some explanation!

By