数组去重
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <script> Array.prototype.unique3 = function(){ var res = []; var json = {}; for(var i = 0; i < this.length; i++){ var objKey = JSON.stringify(this[i]);//把数组值序列化,不然会出bug if(!json[objKey]){ res.push(this[i]); json[objKey] = 1; } } console.log(json); return res; } var arr = [{id:1},{name:'das'},{id:2},{name:0},{id:2},"dsda",{name:'das'}]; console.log(arr.unique3()); var arrNew = [ { id:1, name:'aa' }, { id:1, name:'aa', }, { id:2, name:'bb' }, { id:2, name:{ n:"1" } }, { id:2, name:{ n:"1" } }, { id:2, name:{ n:"2" } }, { id:1, name:'aa' }, { id:1 }, { id:2 }, { id:2 }, 1, 1, [1,2], [1,2], [1,2,] ] console.log(arrNew.unique3()); var arr_1 = [ { id:1, name:[1,2] }, { id:1, name:[1,2] }, { id:1, name:[1,] }, { id:1, name:[1] }, ] console.log(arr_1.unique3()); </script> </body> </html>
相关推荐
-
JavaScript数组知识 javascript
2019-1-8
-
jQuery导出word文档 javascript
2019-1-7
-
js、jQuery各种高度 javascript
2019-1-8
-
移动端、pc端通用点击复制 javascript
2019-1-7
-
jS生成二叉树,二叉树的遍历,查找以及插入 javascript
2019-1-7
-
plupload.js上传到阿里云OSS(前端定义文件名) javascript
2019-1-8
-
js正则验证 javascript
2019-1-8
-
压缩上传图片 javascript
2019-1-7
-
微信小程序wx.request的Promise封装 javascript
2019-1-8
-
SVG 动态添加元素与事件 javascript
2019-1-7