javascript - Push JSON Objects to array in localStorage -


i have function in javascript:

var = []; function savedatatolocalstorage(data) {            var receiveddata = json.stringify(data);     a.push(receiveddata);     alert(a);      localstorage.setitem('session', a);  } 

the data parameter json object.

but everytime click button overwrites data in localstorage.

does know how this?

there few steps need take store information in localstorage. before down code however, please note localstorage (at current time) cannot hold data type except strings. need serialize array storage , parse out make modifications it.

step 1:

the first code snippet below should run if not storing serialized array in localstorage session variable.
ensure localstorage setup , storing array, run following code snippet first:

var = []; a.push(json.parse(localstorage.getitem('session'))); localstorage.setitem('session', json.stringify(a)); 

the above code should run once , if not storing array in localstorage session variable. if doing skip step 2.

step 2:

modify function so:

function savedatatolocalstorage(data) {     var = [];     // parse serialized data aray of objects     = json.parse(localstorage.getitem('session'));     // push new data (whether object or else) onto array     a.push(data);     // alert array value     alert(a);  // should [object array]     // re-serialize array string , store in localstorage     localstorage.setitem('session', json.stringify(a)); } 

this should take care of rest you. when parse out, become array of objects.

hope helps.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -