Home >Web Front-end >JS Tutorial >Code to use json to get the number of occurrences of a character_json

Code to use json to get the number of occurrences of a character_json

WBOY
WBOYOriginal
2016-05-16 17:55:061248browse

The code is as follows:

Copy code The code is as follows:

function ObjRegExp(){
var newStr= "Drive someone up a wall";
newStr = newStr.replace(new RegExp(/s/ig),"");
var objJson = {};
for(var i = 0 ;i < newStr.length ; i ) {
var objChar = newStr.charAt(i);
if(objJson[objChar]){
objJson[objChar] = parseInt(objJson[objChar]) 1;
}else{
objJson[objChar] = 1;
}
}
var oDiv = document.getElementById("objDiv");

oDiv.appendChild (document.createTextNode("Drive someone up a wall!"));
oDiv.appendChild(document.createElement("
"));

for(var key in objJson ){
oDiv.appendChild(document.createTextNode(key ":" objJson[key]));
oDiv.appendChild(document.createElement("
"));
}
}

Html is as follows:
Copy code The code is as follows:





Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn