5

Is there any reason why one should be used over the other?

e.g.

var arData=['a','b','c'];
arData.slice(1,1);//removes 'b'
var arData=['a','b','c'];
delete arData[1];//removes 'b'
Brian Tompsett - 汤莱恩
5,92772 gold badges64 silver badges135 bronze badges
asked Apr 25, 2012 at 23:05
1

2 Answers 2

30

delete leaves you with [ 'a', undefined, 'c' ]

splice leaves you with [ 'a', 'c' ]

slice doesn't do anything to the original array :) But it returns [ 'b' ] in your code

answered Apr 25, 2012 at 23:07
Sign up to request clarification or add additional context in comments.

Comments

6

delete only makes that certain location of the array undefined but the array still contains 3 items: ['a',undefined,'c']

the other way to do it is splice and not slice. splice totally removes that item and it's location, so you end up with ['a','c']

answered Apr 25, 2012 at 23:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.