javascript - Local Storage Error on array push -
i'm doing tests local storage. in tests array, stuck in array part (push).
what i've done:
if (localstorage.myarray) { localstorage.myarray.push("green"); } else { localstorage.myarray = ["red", "blue"]; }
this returns error:
uncaught typeerror: localstorage.myarray.push not function
i know localstorage itens string, don't know how works in array.
since localstorage takes strings, have convert arrays json strings using json.stringify. parse json strings array while performing further operations on "arrays".
if (localstorage.myarray) { var myarray = json.parse(localstorage.myarray); myarray.push("green"); localstorage.myarray = json.stringify(myarray); } else { localstorage.myarray = json.stringify(["red", "blue"]); }
Comments
Post a Comment