代码示例:
var str = "aabbccehgfhaasdhgfashdfhabcasd"; // 使结果显示为一个对象,如:{a:2, b:1, c:2, d:1} var obj = {}; // console.log(obj[ "a" ]) // undefined // obj[ "a" ] = 1 // obj[ "a" ] ++ // obj[ "b" ] = 1 // obj[ "b" ] ++ // obj[ "b" ] ++ // obj[ "b" ] ++ // obj[ "c" ] = 1 for(var i=0;i<str.length;i++){ // obj[ str[i] ] if(obj[ str[i] ]){ obj[ str[i] ]++; }else{ obj[ str[i] ] = 1; } } console.log(obj); var newStr = ""; for(var key in obj){ newStr += key; } console.log(newStr);
推荐教程:js入门教程
以上就是js实现统计字符出现的次数并去重的方法的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号