Javascript 未设置数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2486688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript unset array
提问by Richard Peers
How do i unset an array in javascript? i just want to empty it - so it has nothing in it keys or anything
如何在javascript中取消设置数组?我只想清空它 - 所以它里面没有钥匙或任何东西
回答by marcosbeirigo
you can assign a new array to it:
您可以为其分配一个新数组:
var array = ["element1","element2","element3"];
...
array = new Array();
OR
或者
array = [];
回答by user187291
Assign an empty array to it
为其分配一个空数组
x = []
回答by pms1969
array.length = 0
should work.
应该管用。
回答by James Brooks
What about delete?
删除呢?
delete array
Or to delete a single index:
或者删除单个索引:
delete array[x]
回答by Bj?rn
var array = ["elem", "item"];
...
array = []; // Empty!
回答by Manatax
FOR REFERENCE (I know its old and answered)
供参考(我知道它是旧的并已回答)
If you want to empty the array, that has been answeredalready. If you want to unsetthe array, you could assign an unset variable...
如果你想清空数组,那已经得到了回答。如果要取消设置数组,可以分配一个未设置的变量...
var undef;
var myArr = [];
...
myArr = undef;
// typeof myArr == undefined

