码迷,mamicode.com
首页 > 编程语言 > 详细

js数组清空和去重

时间:2017-04-01 09:19:08      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:prot   length   1.5   height   code   数组   tools   splice   line   

1.splice

var ary = [1,2,3,4];

ary.splice(0,ary.length);

console.log(ary); // 输出 Array[0],空数组,即被清空了

2.length赋值为0

这种方式很有意思,其它语言如Java,其数组的length是只读的,不能被赋值。如

int[] ary = {1,2,3,4}; 
ary.length = 0;

Java中会报错,编译通不过。而JS中则可以,且将数组清空了,

var ary = [1,2,3,4]; 
ary.length = 0; console.log(ary); // 输出 [],空数组,即被清空了

目前 Prototype中数组的 clear 和mootools库中数组的 empty 使用这种方式清空数组。

3.赋值为[]

var ary = [1,2,3,4];
ary = [];

js数组清空和去重

标签:prot   length   1.5   height   code   数组   tools   splice   line   

原文地址:http://www.cnblogs.com/cosyer/p/6654495.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!