利用json获取字符出现次数的代码_json
程序员文章站
2023-12-22 22:40:04
...
代码如下:
function ObjRegExp(){
var newStr= "Drive someone up a wall";
newStr = newStr.replace(new RegExp(/\s/ig),"");
var objJson = {};
for(var i = 0 ;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 如下:
复制代码 代码如下:
function ObjRegExp(){
var newStr= "Drive someone up a wall";
newStr = newStr.replace(new RegExp(/\s/ig),"");
var objJson = {};
for(var i = 0 ;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 如下:
复制代码 代码如下: