javascript - if I am using only integers as keys how can get last inserted key in localstorage? -
i trying build registration form , using localstorage save details. while adding form details locastorage, want know value of last inserted key can increment , can insert new record incremented value of key.
how can done. current code overwriting values if delete record, since taking count of keys available in storage.
function setlocal(){ var o = {}; var ff = $('#myform').serializearray(); $.each(ff, function() { if (o[this.name] !== undefined) { if (!o[this.name].push) { o[this.name] = [o[this.name]]; } o[this.name].push(this.value || ''); } else { o[this.name] = this.value || ''; } }); var tt = json.stringify(o); localstorage.setitem(localstorage.length+1, tt); $('#afterinsert').text('data added sucessfully'); return true; }
any suggestions?
why can't store last inserted key in separate property
var lastkey = parseint(localstorage.getitem('lastkey')); var newkey = lastkey + 1 || 1; localstorage.setitem(newkey , tt); localstorage.setitem('lastkey', newkey);
Comments
Post a Comment