JavaScript从字符串中删除非重复字符
我们需要编写一个JavaScript函数,该函数接受一个字符串并返回一个新字符串,并从中删除所有非重复字符。
例如-
如果输入字符串是-
"teeth_foot"
那么输出应该是-
"teetoot"
因此,让我们为该函数编写代码-
示例
const str = 'teeth_foot';
const removeNonDuplicate = str => {
const strArray = str.split("");
const duplicateArray = strArray.filter(el => {
return strArray.indexOf(el) !== strArray.lastIndexOf(el);
});
return duplicateArray.join("");
};
console.log(removeNonDuplicate(str));输出结果
控制台中的输出将为-
teetoot
热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短