0

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

3
  • 4
    why would you want to use jQuery for such a task? Just use freakin' JavaScript already! stackoverflow.com/questions/5767325/… Commented May 23, 2014 at 0:53
  • I need to search first string.. Commented May 23, 2014 at 0:55
  • you should mark @Kong answer as correct if that helped you. Commented May 23, 2014 at 0:59

1 Answer 1

2

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".

Sign up to request clarification or add additional context in comments.

2 Comments

"c" may be at any place.So First I need to get the index of c ?
Then use indexOf() method along with this one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.