-1

I am trying to update a JSON array of data that is being stored in localStorage but it keeps crashing for some reason and I dont know why. Below is the code:

this.setToken = function(newToken) {
        var settingsList = JSON.parse(window.localStorage.getItem("settings"));
        var l = settingsList.length;
        for (var i=0; i < l; i++) {
            if (settingsList[i].token === "") {
                app.showAlert(newToken,"[TOKEN-DEBUG]");
                settingsList.[i].token = newToken;
                break;
            }
        }
    }

    var settings = [{"token": ""}
                    ];
    window.localStorage.setItem("settings", JSON.stringify(settings));

It keeps erroring on the following line:

settingsList.[i].token = newToken;

Please can someone help me with this as its driving me crazy :S

user723858
  • 1,017
  • 3
  • 23
  • 45

3 Answers3

1

Remove the dot between the array variable and the index.

settingsList[i].token = newToken;
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
1

Just remove the dot .:

settingsList.[i].token = newToken;

Should be

settingsList[i].token = newToken;
Klaus Byskov Pedersen
  • 117,245
  • 29
  • 183
  • 222
1

settingsList.[i].token = newToken;

should be

settingsList[i].token = newToken;
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106