Deleting array elements in JavaScript - delete vs splice

后端 未结 27 3594
予麋鹿
予麋鹿 2020-11-21 05:31

What is the difference between using the delete operator on the array element as opposed to using the Array.splice method?

For example:

myArray = [\         


        
27条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-21 06:14

    you can use something like this

    var my_array = [1,2,3,4,5,6];
    delete my_array[4];
    console.log(my_array.filter(function(a){return typeof a !== 'undefined';})); // [1,2,3,4,6]

提交回复
热议问题