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

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -