Why does the array still have a non-zero length after deletion?

前端 未结 5 2007
后悔当初
后悔当初 2021-01-14 12:14

I have the following code that outputs the length of an array, deletes it, and then outputs the new length:

console.log($scope.adviceList.activeAdvices.lengt         


        
5条回答
  •  不要未来只要你来
    2021-01-14 13:05

    For my part, I redefined the length after delete:

    var cars = ["BMW", "Volvo", "Saab", "Ford"];
    delete cars[1];
    
    /// 1st option ///
    for (var i = 0; i < cars.length; i++){
      if(i in cars)
        document.write(cars[i] + " ");
    }
    /// return: BMW Saab Ford
    
    /// 2nd option ///
    cars.length--;
    var realLength = cars.length;
    

提交回复
热议问题