javascript - push multiple array object into localhost? -


(function{ var array = localstorage.getitem('id') || []; obj = {}; obj.id=1; array.push(obj);  localstorage.setitem('id',json.stringify(array)); }(); 

why in localstorage doesn't insert obj twice? i'm seeing replace existing one.

it replace existing one.

try this:

if(localstorage.getitem('id')){  array = json.parse(localstorage.getitem('id')) } else { var array = []; } obj = {}; obj.id=1; array.push(obj);  localstorage.setitem('id',json.stringify(array)); 

everytime adding first gets localstorage item , adds , puts back.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -