1

I have the following:

var allItems = {Items: [
                        {Value: 1, Name: "String1"},
                        {Value: 2, Name: "String2"},
                        {Value: 3, Name: "String3"}
                       ] 
               };

localStorage.setItem('allItems', allItems);

I'm struggling to access any properties of allItems after retrieving it from localStorage. How would I say access the Name of the second object in the array Items?

OliverJ90
  • 1,291
  • 2
  • 21
  • 42

1 Answers1

3

You have to use JSON stringify to store the object and then use parse to convert it back into a JSON object.

localStorage.setItem('allItems', JSON.stringify(allItems));
var storageItems = JSON.parse(localStorage.getItem('allItems'));
console.log(storageItems.Items[1].Name);
berrberr
  • 1,914
  • 11
  • 16