JavaScript array slice versus delete

后端 未结 2 1402
伪装坚强ぢ
伪装坚强ぢ 2021-01-06 01:47

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\',         


        
2条回答
  •  南方客
    南方客 (楼主)
    2021-01-06 02: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

提交回复
热议问题