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

前端 未结 5 1953
后悔当初
后悔当初 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 12:48

    It's because delete on array element set it to undefined so the length don't change. Use splice function instead:

    a = [1,2,3,4];
    a.splice(2,1);
    console.log(a.length);
    

提交回复
热议问题