1.使用indexOf判断删除
	// 字符串中删除重复的字符
        let str = 'iuuiiuuu';
        // 思路 
        let res = '';
        for(let i=0;i<str.length;i++){
            console.log(str[i]);
            if(res.indexOf(str[i])>-1){
                console.log('存在')
            }else{
                console.log('不存在')
                res += str[i];
            }
        }
        console.log('去重后的字符-->',res);
2.转化为数组 使用ES6中的Set函数去重
		// 思路2 转化为数组
        let arr = str.split('');
        let res = [...new Set(arr)].join('');
        console.log('去重后的字符-->',res);
❤️
split() 方法用于把一个字符串分割成字符串数组。

join() 方法将数组作为字符串返回。
Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐