var arr = [1,2,5,3,4,4,4,4,4,4]; var obj = {} for(var i in arr){ obj[arr[i]] = 100; } console.log( obj);
es6:
var arr = [1,2,3,4,4,2,4,45,6,6,7,7,"dff","dff",'7',{'dd':'dd'}]; console.log(...new Set(arr));
var arr = [5,2,3,6,8,6,5,4,7,1,9]; var arrB = []; for(var i = 0; i<arr.length; i++){ console.log(arr[i]); if(arrB.indexOf(arr[i]) == -1){ arrB.push(arr[i]); } } console.log(arrB);
字符串 去重:
//声明变量 var str = "aaaaaaa2222ffff"; var str1 = ""; var flag = false; for(var i = 0; i < str.length; i++) { var c = str.charAt(i); if(str.indexOf(c) == str.lastIndexOf(c)) { flag = false; } else { flag = true; } if(i == str.indexOf(c)) { flag = true; if(flag) { str1+=c; } } } console.log(str1);