当前位置:网站首页>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 ');
};
边栏推荐
猜你喜欢
antd upload beforeUpload中禁止触发onchange
QT有关QCobobox控件的样式设置(圆角、下拉框,向上展开、可编辑、内部布局等)
Install Jupiter notebook under Anaconda
Chapter 7__ consumer_ offsets topic
Oneforall installation and use
Spark独立集群动态上线下线Worker节点
解决Intel12代酷睿CPU单线程调度问题(二)
(lightoj - 1323) billiard balls (thinking)
sublime text 代码格式化操作
Advancedinstaller安装包自定义操作打开文件
随机推荐
Install Jupiter notebook under Anaconda
Codeforces Round #799 (Div. 4)A~H
OneForAll安装使用
QT realizes window topping, topping state switching, and multi window topping priority relationship
Local visualization tools are connected to redis of Alibaba cloud CentOS server
The "sneaky" new asteroid will pass the earth safely this week: how to watch it
MP4格式详解
拉取分支失败,fatal: ‘origin/xxx‘ is not a commit and a branch ‘xxx‘ cannot be created from it
QT implementation window gradually disappears qpropertyanimation+ progress bar
AcWing:第58场周赛
指定格式时间,月份天数前补零
Chapter 5 yarn resource scheduler
QT implementation fillet window
Market trend report, technical innovation and market forecast of tabletop dishwashers in China
AcWing——第55场周赛
Story of [Kun Jintong]: talk about Chinese character coding and common character sets
CMake Error: Could not create named generator Visual Studio 16 2019解决方法
使用jq实现全选 反选 和全不选-冯浩的博客
Chapter 1 overview of MapReduce
Browser print margin, default / borderless, full 1 page A4