删除数组中的所有元素 - 函数在数组末尾追加 'undefined'
Delete all elements in an array - Function appends 'undefined' at the array end
这是我的代码
var x = [];
function random(min,max) {
return Math.floor(Math.random() * (min-max))+min;
}
function random2(a, b) {
for (let i = 0; i < a; i++) {
x.push(random(0,b));
}
}
random2(5, 100);
console.log(x); // [ -43, -27, -38, -21, -79 ]
x.splice(0, x.length);
x.push(random2(5,100));
console.log(x); // [ -24, -97, -99, -43, -66, undefined ]
我只想删除数组中的所有元素,然后在其中添加新元素。
但是当我尝试使用上面的代码执行此操作时,undefined
也会添加到数组中。
我该如何预防?
将长度设置为零
x.length = 0;
您无需对 return 和 undefined
的函数调用进行处理,而只需调用函数 random2
,因为函数本身会将元素添加到数组中。
function random(min, max) {
return Math.floor(Math.random() * (min - max)) + min;
}
function random2(a, b) {
for (let i = 0; i < a; i++) {
x.push(random(0, b));
}
}
var x = [];
random2(5, 100);
console.log(x);
x.length = 0; // better performance than x.splice(0, x.length)
random2(5,100); // call without using push
console.log(x); // no undefined anymore
更好的方法是return random2
中的数组,因为此函数不访问外部定义的数组。要推送值,您可以采用传播语法。
function random(min, max) {
return Math.floor(Math.random() * (min - max)) + min;
}
function random2(a, b) {
return Array.from({ length: a }, _ => random(0, b));
}
var x = random2(5, 100);
console.log(x);
x.length = 0;
x.push(...random2(5, 100));
console.log(x);
要清空数组,有多种方法,如 here 所述,以及一些基准测试结果和有关其性能的解释。
作为聚合,假设 var a = [1,2,3,4,5]
a = []
a.length = 0
a.splice(0, a.length)
a = new Array()
while(a.pop()){}
while(a.shift()){}
您在 push 方法中调用了函数 random2
。因此 random2
方法首先将值插入数组 x
和 returns 默认值 undefined
(Reference),然后将其推入数组。因此价值。
这是我的代码
var x = [];
function random(min,max) {
return Math.floor(Math.random() * (min-max))+min;
}
function random2(a, b) {
for (let i = 0; i < a; i++) {
x.push(random(0,b));
}
}
random2(5, 100);
console.log(x); // [ -43, -27, -38, -21, -79 ]
x.splice(0, x.length);
x.push(random2(5,100));
console.log(x); // [ -24, -97, -99, -43, -66, undefined ]
我只想删除数组中的所有元素,然后在其中添加新元素。
但是当我尝试使用上面的代码执行此操作时,undefined
也会添加到数组中。
我该如何预防?
将长度设置为零
x.length = 0;
您无需对 return 和 undefined
的函数调用进行处理,而只需调用函数 random2
,因为函数本身会将元素添加到数组中。
function random(min, max) {
return Math.floor(Math.random() * (min - max)) + min;
}
function random2(a, b) {
for (let i = 0; i < a; i++) {
x.push(random(0, b));
}
}
var x = [];
random2(5, 100);
console.log(x);
x.length = 0; // better performance than x.splice(0, x.length)
random2(5,100); // call without using push
console.log(x); // no undefined anymore
更好的方法是return random2
中的数组,因为此函数不访问外部定义的数组。要推送值,您可以采用传播语法。
function random(min, max) {
return Math.floor(Math.random() * (min - max)) + min;
}
function random2(a, b) {
return Array.from({ length: a }, _ => random(0, b));
}
var x = random2(5, 100);
console.log(x);
x.length = 0;
x.push(...random2(5, 100));
console.log(x);
要清空数组,有多种方法,如 here 所述,以及一些基准测试结果和有关其性能的解释。
作为聚合,假设 var a = [1,2,3,4,5]
a = []
a.length = 0
a.splice(0, a.length)
a = new Array()
while(a.pop()){}
while(a.shift()){}
您在 push 方法中调用了函数 random2
。因此 random2
方法首先将值插入数组 x
和 returns 默认值 undefined
(Reference),然后将其推入数组。因此价值。