1 <!DOCTYPE HTML>2 <HTMLLang= "en">3 <Head>4 <MetaCharSet= "UTF-8">5 <title>JS Object find the number of characters and the maximum number of occurrences</title>6 </Head>7 <Body>8 9 </Body>Ten </HTML> One <Script> A varStr= "abcssssssss"; - varJSON= {}; - for(varI= 0; I<str.length; I++) { the var Char =Str.charat (i); - if(json[Char]) { //Char is a property of an object JSON, Json[char] is a property value, Json[char] controls the number of occurrences; - json[Char]++;//Times plus 1 - } Else { + json[Char] = 1; If first appears, the number of times is 1; - } + } A Console.log (JSON);//The output is a complete object that records each character and the number of occurrences//traversal of the object, finding the most frequently occurring characters and times; results: {a:1, b:1, C:1, s:8} at varMax= 0; - varMaxchar= NULL; - for(varKeyinchJSON) { - if(Max<Json[key]) { - Max=Json[key];//Max always stores the largest number of the one; - Maxchar=key;//then the corresponding character is the current key; in } - } to + Console.log ("the maximum number of occurrences is:" +Maxchar+', appearing in total:'+Max+'Times'); - the //Most occurrences are: s, total: 8 times * </Script>
JS Object find the number of characters and the maximum number of occurrences