龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > Javascript编程 >

JS操作数组之返回数组中不重复的元素

时间:2009-12-21 11:47来源:未知 作者:admin 点击:
分享到:
下面使用JS来去掉数组中重复的元素,返回不重复,主要用unique,看例子: Code [http://www.xueit.com] ! doctype html html dir ="ltr" lang ="zh-CN" head meta charset ="utf-8" / meta http-equiv ="X-UA-Compatible" cont

下面使用JS来去掉数组中重复的元素,返回不重复,主要用unique,看例子:

Code [http://www.xueit.com]
<!doctype html> <html dir="ltr" lang="zh-CN"> <head> <meta charset="utf-8"/> <meta http-equiv="X-UA-Compatible" content="IE=7"> <title>unique函数 By 司徒正美</title> <script> Array.prototype.inArray = function(el){ for (var i=0,n=this.length;i<n;i ) if(this[i]===el)return true; return false; } Array.prototype.unique = function() {//取数组中唯一的元素并放进新数组中返回 var i = 0, n = this.length,ret = []; for (; i < n; i ) if (!ret.inArray(this[i])) ret.push(this[i]); return ret; }; var ret= ["span","span","strong","span","b"] alert(ret.unique()) </script> </head> <body> </body> </html>
精彩图集

赞助商链接