3 common ways to empty the array in js

js method to clear array

Click to open the video explanation

The first way: splice

let array = ['a','b','c','d','e'];
array.splice(0,array.length);
console.log(array); // 输出[],空数组,即已被清空

The second way: length assignment is 0

let array = ['a','b','c','d','e'];
array.length = 0;
console.log(array); // 输出[],空数组,即已被清空

The third way: assign the value to [ ], recommended (faster, more efficient)

let array = ['a','b','c','d','e'];
array = []; // 赋值为一个空数组以达到清空原数组的目的

Guess you like

Origin blog.csdn.net/LS_952754/article/details/126009878