Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I have an array like:

var abc = ["a","b","c"];

And the indexes are 0,1,2

Suppose, I want to delete the 2nd item "b" and I the indexes swipe!

Out put:

abc = ["a","c"]

and the indexes are 0,1

How can I achieve this?

share|improve this question

marked as duplicate by NimChimpsky, Salman A, DemoUser, Sani Huttunen, 500 - Internal Server Error Mar 7 '13 at 21:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Are the indexes always 0, 1, and 2? –  Qantas 94 Heavy Mar 7 '13 at 11:12
    
yes, always starts from 0 –  Kannu Mar 7 '13 at 11:14

4 Answers 4

Use the splice function :

abc.splice(1,1) // from index 1, removes 1 element

Be careful that this changes the original array.

share|improve this answer
    
No problem, can you bit explain the parameters? –  Kannu Mar 7 '13 at 11:17
    
I linked to the complete documentation. But here the first argument is the position and the second one the number of elements to remove. –  Denys Séguret Mar 7 '13 at 11:17
1  
+1 but would prefer wording the other way around (from index 1, removes 1 element) –  Paul S. Mar 7 '13 at 11:21
    
@PaulS. Good idea. I edited (you could have to). –  Denys Séguret Mar 7 '13 at 11:24
1  
@PaulS. I just had a look. I'm not surprised to see you don't edit too much I see. But in this precise case I don't feel the need for the cloning addition, as "I like concise, easy-to-read questions and answers" :p –  Denys Séguret Mar 7 '13 at 11:40

Use splice(). E.g:

abc.splice(1, 1);

would perform what you wanted in your example. abc[1] would now be "c".

share|improve this answer

You can use the array splice abc.splice(1,1);

Details: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/splice

share|improve this answer

Have a look on it... I think this is what you want...

var arr = ["a","b","c"];

arr.splice(1,1);

alert("["+arr.indexOf('a')+","+arr.indexOf('c')+"]");
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.