splice and slice the difference

  Thing in common: all are deleted array elements and returns the result.

 

  Difference: splice will change the original array, but will not slice. And splice arrays can lead to collapse.

   Array collapse: when using splice delete elements, the remaining elements of the array index cis hearing will change.

  

let ary = [10,20,30]
for(let i=0;i<ary.length;i++)
{
  splice(i,1);      
}
console.log(ary);//20

When you remove the first element, the element behind the index will lead to the index ahead of the original 20 from the 1 to a 0, resulting in no delete this element 20, which is an array of collapse.

Guess you like

Origin www.cnblogs.com/angle-xiu/p/11318958.html