1

I have an application in which i am storing values in localstorage. By default my first value is null due to which i am getting error to run the code so i want to remove the first element and continue the processes. can anyone please help me how to remove first element from list?

Piece of my code is below:

var str = localStorage.getItem("appidlist");
var mySplitResult = str.split(",");
for(var i = 0; i < mySplitResult.length; i++) {
   if (.....) {
      .
      .
   }    

}

where str returns null,1,2,3,..... I hope my question is clear can anybody help me.

android phonegap
  • 830
  • 3
  • 17
  • 35

4 Answers4

1

This should also work:

var str = localStorage.getItem("appidlist");
var mySplitResult = str.split(",");
mySplitResult.splice(0, 1);
demsey
  • 130
  • 2
  • 6
0

This should do the trick:

var str = localStorage.getItem("appidlist");
var mySplitResult = str.split(",").splice(1); // Remove the first item.
for(var i = 0; i < mySplitResult.length; i++) {
    // Stuff
}
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • hi thank you for the response but it not solved the thing i am still getting this error An error has occurred: 'null' is not an object (evaluating 'str.split') – android phonegap Feb 04 '13 at 09:23
  • Whoops, `.shift()` should have been `.splice(1)`, try that. However, I think the problem might be that `str` is `null`. Are you certain that the `localStorage` contains a item with the name `appidlist` ? – Cerbrus Feb 04 '13 at 09:26
0

You can use .shift().

mySplitResult.shift()
pktangyue
  • 8,326
  • 9
  • 48
  • 71
0

Instead of uing the shift() function, I would suggest you to create a function that return a new clean array. This will solve even the case where there are more than one (and in any postion) null value.

You can use this solution

Community
  • 1
  • 1
Gianni B.
  • 2,691
  • 18
  • 31