android phonegap
android phonegap

Reputation: 830

How to remove a null element in jquery?

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.

Upvotes: 1

Views: 137

Answers (4)

Cerbrus
Cerbrus

Reputation: 72857

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
}

Upvotes: 0

Gianni B.
Gianni B.

Reputation: 2731

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

Upvotes: 0

pktangyue
pktangyue

Reputation: 8524

You can use .shift().

mySplitResult.shift()

Upvotes: 0

demsey
demsey

Reputation: 128

This should also work:

var str = localStorage.getItem("appidlist");
var mySplitResult = str.split(",");
mySplitResult.splice(0, 1);

Upvotes: 1

Related Questions