Javascript 如何从数组中删除元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11543602/
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
How to remove elements from Array?
提问by Ranjith
Possible Duplicate:
how to empty an array in JavaScript
可能的重复:
如何在 JavaScript 中清空数组
How to remove all items from jQuery array?
如何从 jQuery 数组中删除所有项目?
I have array var myArray = [];
, I want to clear all items in this array on every post back.
我有数组 var myArray = [];
,我想在每次回帖时清除此数组中的所有项目。
回答by Pointy
Simplest thing to do is just
最简单的事情就是
myArray = [];
again.
再次。
edit— as pointed out in the comments, and in answers to other questions, another "simplest thing" is
编辑- 正如评论中所指出的,以及在其他问题的回答中,另一个“最简单的事情”是
myArray.length = 0;
and that has the advantage of retaining the same array object.
这具有保留相同数组对象的优点。
回答by blueiur
you can remove all item in myArray using array length, it's common pattern.
您可以使用数组长度删除 myArray 中的所有项目,这是常见的模式。
try this
尝试这个
var myArray = [1, 2, 3];
myArray.length = 0; // remove all item
回答by jbabey
There is no such thing as a jQuery array, that's just a javascript array. When a page posts back, it re-renders and all of the javascript is re-run, you don't need to clear the contents of the array.
没有 jQuery 数组这样的东西,它只是一个 javascript 数组。当页面回发时,它会重新呈现并重新运行所有 javascript,您不需要清除数组的内容。
if, during execution of the page, you wanted to clear a javascript array, just re-initialize it as a new, blank array:
如果在页面执行期间,您想清除一个 javascript 数组,只需将其重新初始化为一个新的空白数组:
myArray = []; // no var, we are just initializing not declaring
回答by Napolux
To clear the array values you can do a simple:
要清除数组值,您可以执行一个简单的操作:
myarray = [];
P.s.
ps
jQuery != javascript
回答by Evan Larsen
Here is a list of methodsyou can do on an Array in javascript
这是您可以在 javascript 中对数组执行的方法列表