Previously, I introduced the use of the charAt() method in JavaScript. Today, I will share with you the example code of js to count the characters and times with the highest number of times. The specific content is as follows: Determine the character that appears most frequently in a string 'aabcdobdackoppz' and count its number of occurrences First we need to understand a knowledge point: (how to determine whether the object has this attribute) // There is an object to determine whether it has this attribute // Writing method: object['attribute name'] var o = { age : 18 , sex : 'male' } if(o['sex']){ console.log('This property exists'); } else { console.log('No such property'); }
1. Use the charAt method to traverse the array 'aabcdobdackoppz' (which can be understood as storing the value in the object obj) <script> var str = 'aabcdobdackoppz' ; var obj = {} ; for(var i = 0 ; i < str.length ; i++){ var chars = str.charAt(i) // chars is each character of the string if(obj[chars]){ // o[chars] gets the attribute obj[chars]++ } else { obj[chars] = 1 ; } } console.log(obj); </script> At this point, the value has been stored in the obj object. 2. Next, you just need to traverse the object to get the maximum value and number of occurrences <script> //Traverse the object var max = 0; var ch = ''; for (var k in obj) { // To traverse an object, you need to use for in // k gets the attribute name, obj[k] gets the attribute value // The attribute name k here is only useful in the loop, so we need to create a ch to store k if (obj[k] > max) { max = obj[k]; ch = k; } } console.log(ch + 'The most frequent occurrence' + 'The number of occurrences is:' + max); </script>
This is the end of this article about using charAt() in JavaScript to count the most frequently appearing characters and their number of times. For more information about how to count the most frequently appearing characters and their number of times in js, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: MySQL 8.0.18 installation and configuration method graphic tutorial under MacOS
>>: Detailed explanation of nginx forward proxy and reverse proxy
Preface Every good habit is a treasure. This arti...
This article shares with you a detailed tutorial ...
This article introduces the development environme...
Using mask layers in web pages can prevent repeat...
The META tag, commonly referred to as the tag, is...
I installed MySQL on Windows by unzipping the com...
This article shares the specific code of Vue to i...
1. scale() method Zoom refers to "reducing&q...
Table of contents 1. The role of watch in vue is ...
MySQL 8 brings a brand new experience, such as su...
The previous article introduced a detailed exampl...
deepin and Ubuntu are both distributions based on...
How to implement text icons through CSS /*icon st...
1. View the detailed information of all current c...
Deployment environment: Installation version red ...