Deleting array elements in JavaScript - delete vs splice

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

    function deleteFromArray(array, indexToDelete){
      var remain = new Array();
      for(var i in array){
        if(array[i] == indexToDelete){
          continue;
        }
        remain.push(array[i]);
      }
      return remain;
    }
    
    myArray = ['a', 'b', 'c', 'd'];
    deleteFromArray(myArray , 0);
    

    // result : myArray = ['b', 'c', 'd'];

提交回复
热议问题