当前位置:网站首页>Soft music -js find the number of times that character appears in the string - Feng Hao's blog
Soft music -js find the number of times that character appears in the string - Feng Hao's blog
2022-07-06 16:39:00 【Feng Hao (grow up)】
Functions used :charAt(): Take the string according to the position
Ideas : We loop the string through charAt Find the character by subscript ,
Then it is stored in an object object name , Then find the attribute value corresponding to the object ,
If there is this object name , Just add one ,
If not, set it to 1
Finally, traverse this object , You can get it
var a = "123abcdefaaaaaaaaaaaaaaaaaaaa";
// Create an array to store elements
var obj = new Object();
// Loop the length of the entire string
for(var i =0;i<a.length;i++){
// charAt: Take the string according to the position
// Loop to remove string
var value = a.charAt(i);
console.log(value);
if(obj[value]>0){
obj[value]++;
}else{
obj[value]=1;
}
}
console.log(obj);
for(var j in obj){
console.log(j+" Number of occurrences "+obj[j]+' Time ');
};
边栏推荐
- Summary of FTP function implemented by qnetworkaccessmanager
- 两个礼拜速成软考中级软件设计师经验
- 生成随机密码/验证码
- Market trend report, technical innovation and market forecast of tabletop dishwashers in China
- 第 300 场周赛 - 力扣(LeetCode)
- QT simulates mouse events and realizes clicking, double clicking, moving and dragging
- Base dice (dynamic programming + matrix fast power)
- 力扣——第298场周赛
- Chapter 5 detailed explanation of consumer groups
- <li>圆点样式 list-style-type
猜你喜欢
随机推荐
Educational Codeforces Round 130 (Rated for Div. 2)A~C
(POJ - 1458) common subsequence (longest common subsequence)
Educational Codeforces Round 130 (Rated for Div. 2)A~C
Market trend report, technological innovation and market forecast of desktop electric tools in China
图像处理一百题(1-10)
力扣:第81场双周赛
Summary of FTP function implemented by qnetworkaccessmanager
Problem - 922D、Robot Vacuum Cleaner - Codeforces
AcWing:第58场周赛
Market trend report, technical innovation and market forecast of double-sided foam tape in China
Codeforces - 1526C1&&C2 - Potions
300th weekly match - leetcode
js封装数组反转的方法--冯浩的博客
Investigation report of bench type Brinell hardness tester industry - market status analysis and development prospect prediction
计算时间差
Generate random password / verification code
Codeforces round 797 (Div. 3) no f
Codeforces Round #801 (Div. 2)A~C
Codeforces Round #801 (Div. 2)A~C
Submit several problem records of spark application (sparklauncher with cluster deploy mode)