手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >JS数组去重与取重的示例代码
JS数组去重与取重的示例代码
摘要:方法一:去重复数据复制代码代码如下:Array.prototype.distinct=function(){vara=[],b=[];for...

方法一:去重复数据

复制代码 代码如下:

<script>

Array.prototype.distinct=function(){

var a=[],b=[];

for(var prop in this){

var d = this[prop];

if (d===a[prop]) continue; //防止循环到prototype

if (b[d]!=1){

a.push(d);

b[d]=1;

}

}

return a;

}

var x=['a','b','c','d','b','a','e','a','b','c','d','b','a','e'];

document.write('原始数组:'+x);

document.write("<br />");

document.write('去重复后:'+x.distinct());

</script>

方法二:取重复数据

复制代码 代码如下:

<script type="text/javascript">

Array.prototype.distinct=function(){

var a=[],b=[],c=[],d=[];

for(var prop in this){

var d = this[prop];

if (d===a[prop])

{

continue;

}//防止循环到prototype

if (b[d]!=1){

a.push(d);

b[d]=1;

}

else {

c.push(d);

d[d]=1;

}

}

//return a;

return c.distinct1();

}

Array.prototype.distinct1=function(){

var a=[],b=[];

for(var prop in this){

var d = this[prop];

if (d===a[prop]) continue; //防止循环到prototype

if (b[d]!=1){

a.push(d);

b[d]=1;

}

}

return a;

}

var x=['a','b','c','d','b','a','e','a','b','c','d','b','a','e','f','f','g'];

document.write('原始数组:'+x);

document.write("<br />");

document.write('去重复后:'+x.distinct());

</script>

【JS数组去重与取重的示例代码】相关文章:

js去除字符串里中文与空格的例子

JavaScript实现广告的关闭与显示效果实例

Ctrl + Enter提交前检测的代码

JavaScript数据结构与算法之链表

javascript中FOREACH数组方法使用示例

根据分辨率不同,调用不同的css文件

Java数据类型以及变量的定义

JavaScript数组去重的3种方法和代码实例

打印/预览/设置的客户端代码

JavaScript判断数组是否包含指定元素的方法

精品推荐
分类导航