'How do I convert a list to local storage and convert back to the same variable?

    let grid = [];
    function redirect()
    {
        localStorage.setItem("newGrids", JSON.stringify(grid));
        window.location.href = 'easterEgg.html';
    }
    
    function makeTable()
    {
        if (localStorage.newGrids) 
        {
            var retrievedData = localStorage.getItem("newGrids");
            grid = JSON.parse(retrievedData);
        {
        else 
       {
            for (let row = 0; row < numRows; row++)
            {
               let temp = [];
               for (let col = 0; col < numCols; col++)
            {
               temp.push(new GuessCell('-'));
            }
               grid.push(temp);
           }   
      }

Here is the code. I want to assign the value of "grid" to "newGrids"(in local storage) when the code goes to a new page and then put the value back into "grid" when the page loads again. The code is not saving the grid when run.



Solution 1:[1]

This line feels odd in the code. You can't use the localStorage object like this.

if (localStorage.newGrids)

Other than that the code looks fine. Unless the rows are not having any data, the grid value would have stored.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Susindar