6

I have some values that are dynamically stored at localStorage with incremented values like this: localStorage["Value0"], localStorage["Value1"],....

When I try to access them like this:

JavaScript:

localStorage["Counter"]=0;
var i = localStorage["Counter"]; 
var d =localStorage["Value"+i];
i = i + 1; // i becomes "01"
var f = localStorage["Value"+i];

The i's value is "01" not 1 ... Is there a way to increment the i's value correctly?

Sana Joseph
  • 1,948
  • 7
  • 37
  • 58

3 Answers3

17

LocalStorage can only store string values. You can use parseInt which converts a string into an integer:

var new_value =  parseInt(localStorage.getItem('num')) + 1

You can also use libraries like store.js to do things automatically for you. All you have to do is to include the library:

<script src="store.js"></script>

Set a new storage:

var numbers = new Store("numbers")

Put things into it:

numbers.set('num', 2)

Get the value and do anything you want with it:

numbers.get('num') + 1 //output: 3

And you can also go crazy and use some arrays:

numbers.set('nums', [1,2,3])

And change things inside it:

numbers.get('nums')[0] + 3 //output: 4

No type conversion needed. You can also store objects, booleans and other stuff. Just don't forget to save things back in the storage since it doesn't automatically do it.

Wern Ancheta
  • 22,397
  • 38
  • 100
  • 139
2

To fix the problem of getting 01, simply convert the variable to a number:

var i = +localStorage.getItem('Counter'); // or +localStorage.Counter;

If you want an incremental key name, the following code can also be used (provided that you don't delete keys in between):

var keyname = 'Value' + localStorage.length;
localstorage.setItem(keyname, value);
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • You can convert a string to a number with `parseInt()` – Patrick Oscity Jul 07 '12 at 13:35
  • 1
    you can also do `i = i | 0;` to convert to integer – Boris Guéry Jul 07 '12 at 13:35
  • 2
    @padde `1*`, `+`, `~~`, and [many others](http://stackoverflow.com/a/8112802/938089?are-there-are-any-side-effects-of-using-this-method-to-convert-a-string-to-an-in), depending on the conditions. – Rob W Jul 07 '12 at 13:35
  • I have a lot of values stored at localStorage, what is the length that's supposed to be returned ? – Sana Joseph Jul 07 '12 at 13:40
  • 1
    @SanaJoseph The number of keys. For example, when you've got an empty storage, and use `localStorage.setItem('x','y');`, then `localStorage.length === 1`. – Rob W Jul 07 '12 at 13:43
0

localStorage always returns string values. So to iterate them, I think it's better to use the conversion method. Here you can increment the key value in localStorage.

let k = 0;
k = k.toString();
localStorage.setItem(k, "ahmad");
k = parseInt(k);
k++;
k = k.toString();
localStorage.setItem(k, "bilal");

You can call the localStorage.getItem() method like this:

k = 0;
k = k.toString();
console.log(localStorage.getItem(localStorage.key(k)));
k = parseInt(k);
k++;
k = k.toString();
console.log(localStorage.getItem(localStorage.key(k)));
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459