locked
Saving arrays in localStorage with js RRS feed

  • Question

  • I am developing a JS App for Wndows 8.1, and I want to store some arrays in localStorage. I run the following code:

    if (localStorage.abilities == undefined) {

                         localStorage.abilities=new Array();

                for(*whatever*){

                       localStorage.abilities[i]=1;

                  }

            }

    But, when i debug,instead of geting [1,1,1,1...], I get "1,1,1,....", a string containing the numbers separated by commas. Also, when I try to edit a single element (localStorage.abilities[j]=4;), it has no effect. Why does it happen?

    Tuesday, December 10, 2013 11:42 AM

Answers

  • Hi ArcadioGarcia,

    Take a look at the documentation from W3shcool: http://www.w3schools.com/html/html5_webstorage.asp, the following words might be the reason that you cannot get [1,1,1,1] but a string, you need convert it before using.

    Tip: Key/value pairs are always stored as strings. Remember  to convert them to another format when needed.

    By the way, the default way to do this in Windows Store App is to use WinJS.Application.local object

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Thursday, December 12, 2013 8:24 AM
    Moderator