I use local storage to save the game, I am going to save the array, but it needs initial values ​​unequal to zero, how to implement?

  • one
    ему нужны - to whom he? как осуществить? - to accomplish what? - Alexey Shimansky
  • I need values ​​to be there - Lspewz
  • там is it? - Alexey Shimansky
  • @ Alexey Shimansky, in the array, obviously - Grundy
  • @Grundy where did you get it?) You can contain zero there too, which means it's not clear where) - Alexey Shimansky

1 answer 1

Apparently you want something like this:

 // Начальный массив var initArray = [555,666,777]; // Загрузка игровых данных, где возвращается либо массив из localStorage // либо если его не существует еще, то загружается initArray var gameData = loadGameData(initArray); document.write(gameData[0] + "<br/>"); document.write(gameData[1] + "<br/>"); document.write(gameData[2] + "<br/>"); function loadGameData(initArray) { return getGameData() || initArray; } // Возвращает данные из localStorage, если он существует function getGameData() { if (localStorage.getItem('gameData')) return localStorage.getItem('gameData').split(','); return false; } // Какой-то игровой процесс // ... // ... // ... // Сохранение данных gameData = [1,2,3]; localStorage.setItem('gameData', gameData); 

In general, an array is not the most convenient thing to store in localStorage . Usually they keep Json , which I would advise.