javascript - How to increment a value at localStorage -
i have values dynamically stored @ localstorage incremented values this: localstorage["value0"], localstorage["value1"],....
when try access them this:
javascript:
localstorage["counter"]=0; var = localstorage["counter"]; var d =localstorage["value"+i]; = + 1; // becomes "01" var f = localstorage["value"+i]; the i's value "01" not 1 ... there way increment i's value correctly?
localstorage can store string values. can use parseint converts string integer:
var new_value = parseint(localstorage.getitem('num')) + 1 you can use libraries store.js things automatically you. have include library:
<script src="store.js"></script> set new storage:
var numbers = new store("numbers") put things it:
numbers.set('num', 2) get value , want it:
numbers.get('num') + 1 //output: 3 and can go crazy , use arrays:
numbers.set('nums', [1,2,3]) and change things inside it:
numbers.get('nums')[0] + 3 //output: 4 no type conversion needed. can store objects, booleans , other stuff. don't forget save things in storage since doesn't automatically it.
Comments
Post a Comment