本文介绍了JS实现阵列重复数据消除的方法。分享给大家参考,如下:
运行渲染如下:
完整的示例代码如下:
!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN ' ' http://www . w3 . org/TR/XHTML 1/DTD/XHTML 1-Transitional . DTD ' html xmlns=' http://www . w3 . org/1999/XHTML ' hearteta http-equiv=' Content-Type ' Content=' text/html;charset=utf-8 '/title TeSt/title script type=' text/JavaScript ' language=' JavaScript ' array . prototype . distinct=function(){ var $=this;var O1={ };//存储重复数据删除值var O2={ };//存储重复值var O3=[];//存储重复值var o;//数组单变量为(var I=0;o=$[I];i ){ if(o in o1){ if(!(o2中的o))O2[o]=o;删除$[I];} else { O1[o]=o;} } $.长度=0;//清除(o1中的o){ $ }的原始数组。push(o);} for(o in O2){ O3 . push(o);}返回O3;}var a=[2,2,2,3,3,3,4,4,5,6,7,7];Console.log('原始数组:' a ');//2,2,2,3,3,4,4,5,6,7,7 console.log('重复的元素是:' a . distinct());//2,3,4,7console.log('排序后的数组为:' a ');//2,3,4,5,6,7console.log('整理后长度为: ' a . length)//6/script/head dy/body/html PS:在这里,我们将为您提供几个重复数据删除工具,供您参考:
在线重复数据消除工具:http://tools.jb51.net/code/quchong
在线文本重复数据消除工具:http://tools.jb51.net/aideddesign/txt_quchong
更多对JavaScript相关内容感兴趣的读者可以查看本网站专题:《JavaScript数组操作技巧总结》、《JavaScript排序算法总结》、《JavaScript数学运算用法总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》、《JavaScript查找算法技巧总结》、《JavaScript错误与调试技巧总结》和0103010
希望本文对JavaScript编程有所帮助。