js统计字符出现的次数

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head> <body> <script>
    输入一个字符串,统计该字符串中每个字符出现的次数 

    var str = "aabbccddeeef";
    var strNew = {};创建一个空的对象
    for(var i=0; i<str.length; i++){
        var num = str[i];
        if (!strNew[num]) {
            strNew[num] = 1;
        }else {
            strNew[num] += 1;
        }
    }
    console.log(strNew);
</script>
</body>
posted on 2023-04-05 15:50  hqingxiang  阅读(1099)  评论(0)    收藏  举报