js去除数组重复项以及js移除json里面的重复项的方法
本文发布于 13 年前, 内容可能已经过时或失效!
js 去除里面重复的项 实际上原理很简单 下面是代码 ```html <!DOCTYPE HTML> <html> <head> <title>js去除数组重复项以及js移除json里面的重复项的方法</title> <meta charset="UTF-8"> <meta name="author" content="yanue"/> <meta name="copyright" content="www.yanue.net"/> </head> <body> <script type="text/javascript"> //去除数组重复 //var arr = ['a','b','c','a','b','d','b']; //去除json数组里面重复 var arr = [ { 'cid': 1, 'county': '小河区' }, { 'cid': 1, 'county': '小河区' }, { 'cid': 2, 'county': '南明区' }, { 'cid': 1, 'county': '小河区' }, { 'cid': 1, 'county': '小河区' }, { 'cid': 3, 'county': '乌当区' }, { 'cid': 1, 'county': '小河区' } ] var hash = {} for (var i = 0; i < arr.length; i++) { // 去除数组重复情况 // (hash[arr[i]] == undefined) && (hash[arr[i]]=arr[i]); // 去除json数组里面重复情况 (hash[arr[i]] == undefined) && (hash[arr[i]['cid'] + ',' + arr[i]['county']] = arr[i]['cid'] + ',' + arr[i]['county']) } for (var o in hash) { cid = o.split(',')[0] county = o.split(',')[1] document.write(cid + ' ' + county + '<br>') } </script> </body> </html> ```