javascript - How to stock array with localStorage (Chrome Extension)? -


i tried stock array in localstorage read impossible. tried :

    array = {};     array.name =        $('[name="name"]').val();     array.username =    $('[name="username"]').val();     array.password =    $('[name="password"]').val();      alert(localstorage['accounts']);     local = json.parse(localstorage['accounts']);     localu = local.push(array);     alert(json.stringify(localu)); 

in fact scripts stops @ first alert wich returns '[]' (i put value check result).

why isn't script working ?

javascript, {} object. [] array.

var array = [] , var array = new array() same thing.

an array ordered container of stuff, each value has index not key.

an object named container of stuff, each "stuff" has key.

your array object.

var data = {}; data.name =        $('[name="name"]').val(); data.username =    $('[name="username"]').val(); data.password =    $('[name="password"]').val();  alert(localstorage['accounts']); // > undefined or value local = json.parse(localstorage['accounts']); // local contains parsed version of localstorage['accounts'] localu = local.push(array); // localu = 0 (push returns length think?) alert(json.stringify(localu)); 

try following. i've not tested it, might work.

var data = {}; data.name =        $('[name="name"]').val(); data.username =    $('[name="username"]').val(); data.password =    $('[name="password"]').val();  if (localstorage['accounts'] == undefined) { // fixed // key exist? no create started    localu = { accounts: [] }; } else { // key exists! lets parse    localu = json.parse(localstorage['accounts']); } // add new "data" list localu.accounts.push(data); // save results (we have stringify it) localstorage['accounts'] = json.stringify(localu); 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -