How to empty an javascript array?

var arr = [-3, -34, 1, 32, -100];

How can I remove all items and just leave an empty array?

And is it a good idea to use this?

arr = [];

Thank you very much!


If there are no other references to that array, then just create a new empty array over top of the old one:

array = [];

If you need to modify an existing array—if, for instance, there's a reference to that array stored elsewhere:

var array1 = [-3, -34, 1, 32, -100];
var array2 = array1;

// This.
array1.length = 0;

// Or this.
while (array1.length > 0) {
    array1.pop();
}

// Now both are empty.
assert(array2.length == 0);

其中之一:

var a = Array();
var a = [];

正如你所说:

arr = [];
链接地址: http://www.djcxy.com/p/27176.html

上一篇: 在Phaser中摧毁精灵

下一篇: 如何清空JavaScript数组?