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

Popular posts from this blog

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -

javascript - Restarting Supervisor and effect on FlaskSocketIO -

php - Mongodb connectivity error -