can you please tell me how to how to delete value from array using jquery ?.I am able to delete values But in place of value I am getting undefined value.
items = ['a', 'b', 'c', 'd'];
if(items.indexOf('c') !== -1) {
delete items[items.indexOf('c')];
}
console.log(items)
alert(items)
alert(items.length)
It is printing 4 length.It is taking undefined value in array.How to remove completely from array ? So that it length become 3.and out put become a,b,d
asked May 23, 2014 at 0:51
Pallavi Sharma
6552 gold badges16 silver badges47 bronze badges
-
4why would you want to use jQuery for such a task? Just use freakin' JavaScript already! stackoverflow.com/questions/5767325/…Saturnix– Saturnix2014年05月23日 00:53:17 +00:00Commented May 23, 2014 at 0:53
-
I need to search first string..Pallavi Sharma– Pallavi Sharma2014年05月23日 00:55:22 +00:00Commented May 23, 2014 at 0:55
-
you should mark @Kong answer as correct if that helped you.Saturnix– Saturnix2014年05月23日 00:59:14 +00:00Commented May 23, 2014 at 0:59
1 Answer 1
Use JavaScript Array's built in splice method:
array.splice(index, 1);
The second parameter is the number of elements to remove, so 1 = "just this one".
answered May 23, 2014 at 0:54
Kong
9,72417 gold badges77 silver badges115 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Pallavi Sharma
"c" may be at any place.So First I need to get the index of c ?
Spencer Wieczorek
Then use
indexOf() method along with this one.lang-js