Search code examples
jqueryarraysjsonupdating

Updating a JSON object using JQuery error?


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


Solution

  • Remove the dot between the array variable and the index.

    settingsList[i].token = newToken;