0

I have the following javascript objects. I would like to store the following array on sessionstorageItem, but it was giving me error. How can I able to store an array in sessionstorageItem?

data=[];
data[0] = [{
    "num": 29,
    "ser": 1,
}, {
    "num": 44,
    "ser": 2,
}] 
data[1]=[{
    "num": 10,
    "ser": 3,
}]

 allData = data.reduce(function (a, b) { return a.concat(b) });
  // the following line gives me an error
 var MDData=JSON.parse(sessionStorage.MDData);
 if (MDData!==null) {console.log("Hello")}
 sessionStorage.setItem('MyData', JSON.stringify(allData));
mystackoverflow
  • 357
  • 1
  • 4
  • 10

2 Answers2

1

webStorage can only store strings. So you need to stringify your data.

sessionStorage.setItem('Myata', JSON.stringify(allData));

To retrieve it back you can do:

JSON.parse(sessionStorage.Myata);
Zee
  • 8,420
  • 5
  • 36
  • 58
0

you have and extra ')' in "sessionStorage.setItem('Myata', sell.allData));"

PauAI
  • 384
  • 4
  • 16