We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Learn more about funding links in repositories.
Report abuse
There was an error while loading. Please reload this page.
第173天 清空一个数组的方式有哪些?它们有什么区别?
arr.length = 0;
arr.splice(0,arr.length);
arr = [];
a = Array.of()
这是完美的,因为这实际上创建了一个全新的(空)数组 仅当您仅通过数组的原始变量A引用数组时才使用此选项。
let arr1 = [1,2,3] let arr2 = arr1 arr1 = [] console.log(arr1,arr2) // [] [1,2,3]
通过将现有数组的长度设置为 0 来清除该数组 会影响元数组
const arr1 = [1,2,3] const arr2 = arr1 arr1.length = 0 console.log(arr1,arr2) // [] []
.splice()函数将返回一个包含所有已删除项的数组,因此它实际上将返回原始数组的副本
const arr1 = [1,2,3] const arr2 = arr1 arr1.splice(0,arr1.length) console.log(arr1,arr2) // [] []
最慢的方法
const arr1 = [1,2,3] const arr2 = arr1 while (arr1.length > 0) { arr1.pop() } console.log(arr1,arr2) // [] []
arr = [] ; arr.length = 0 ;
Activity
coconilu commentedon Oct 6, 2019
arr.length = 0;
Liuwan12 commentedon Oct 7, 2019
arr.splice(0,arr.length);
Via1877 commentedon Oct 8, 2019
arr = [];
nyz123 commentedon Oct 9, 2019
a = Array.of()
ZindexYG commentedon May 29, 2020
方法1
这是完美的,因为这实际上创建了一个全新的(空)数组
仅当您仅通过数组的原始变量A引用数组时才使用此选项。
方法2
通过将现有数组的长度设置为 0 来清除该数组
会影响元数组
方法3
.splice()函数将返回一个包含所有已删除项的数组,因此它实际上将返回原始数组的副本
方法4
最慢的方法
xiaoqiangz commentedon Aug 4, 2022
arr = [] ; arr.length = 0 ;