Deleting array elements in JavaScript - delete vs splice

后端 未结 27 3720
予麋鹿
予麋鹿 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:19

    If the desired element to delete is in the middle (say we want to delete 'c', which its index is 1), you can use:

    var arr = ['a','b','c'];
    var indexToDelete = 1;
    var newArray = arr.slice(0,indexToDelete).combine(arr.slice(indexToDelete+1, arr.length))
    

提交回复
热议问题