首页 > 代码库 > 数组去重
数组去重
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="http://www.mamicode.com/http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js "> </script> </head> <body> <input type="text" /> <button>点击添加数据</button> </body> </html> <script> Array.prototype.unique3 = function(){ var res = []; var obj = {}; for(var i = 0; i < this.length; i++){ if(!obj[this[i]]){ res.push(this[i]); obj[this[i]] = 1; } } return res; } var arr = []; $(‘button‘).click(function() { var val = $(‘input‘).val(); arr.push(val); var result=arr.unique3(); console.log(result); }) </script>
小石头写的添加重复还要有弹窗提醒:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="http://www.mamicode.com/http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js "> </script> </head> <body> <input type="text" class="inputtext" /> <button>点击添加数据</button> </body> </html> <script> Array.prototype.unique3 = function() { var res = []; var obj = {}; for(var i = 0; i < this.length; i++) { if(!obj[this[i]]) { res.push(this[i]); obj[this[i]] = 1; } } return res; } var arr = []; var result = []; $(‘button‘).click(function() { var val = $(‘input‘).val(); if(val.length!=0){ for(var i = 0; i < result.length; i++) { if(val == result[i]) { alert(‘重复了‘); } }; arr.push(val); result = arr.unique3(); console.log(result); } }) </script>
数组去重
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。