直接贴代码
- ES5设置对象判断去重
var str = 'kkkkkkkskdksdsddssusfebusejrfsssssasasdaaaaaaaaadsdssssssssss';
var delReplyChar = function (str) {
var arr = str.split('');
var result = {};
var resultArr = [];
for (var i = 0; i < arr.length; i++) {
if(!result[arr[i]]){
result[arr[i]] = arr[i];
resultArr.push(arr[i]);
}
}
return resultArr.join('');
};
console.log(delReplyChar(str));
控制台输出:
ksdufebjra
- ES6数组去重
var str = 'kkkkkkkskdksdsddssusfebusejrfsssssasasdaaaaaaaaadsdssssssssss';
var arr = str.split('');
var set = new Set();
var result = [...new Set(arr)];
//or ,,result = Array.from(new Set(arr));
console.log(result);