当前位置:网站首页>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 ');
   };

 Insert picture description here

原网站

版权声明
本文为[Feng Hao (grow up)]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060920252406.html