计算字符串内的字出现几次

    // 创建一个字符串 
    var str = "adminadmiaaaanhelloworldhello",
        obj = {};
    console.log(str, obj)
    console.log(undefined + 1)
    // 开启循环处理字符串
    for (var i = 0; i < str.length; i++) {
        // str[i] --> str[0] = a --> obj[a]:10 
        // obj[a] = (obj[str[i]] + 1) || 1;
        // obj[a] = 1
        // obj[a] = (obj[str[i]] + 1) || 1;
        // console.log(obj[str[i]] )
        // obj[str[i]] = (obj[str[i]] + 1) || 1;

        // obj[str[i]] = 1;
        // key:value //左边的是key 右边的是value 赋值给key
        // obj[a] = obj[a]+1 ||1;
        obj[str[i]] = (obj[str[i]]) + 1 || 1
    }

    //

    // console.log(obj)
    // 对象.方法|属性 
    var v_json = JSON.stringify(obj); // 静态方法   
    console.log(v_json) // ->
    //{"a":6,"d":3,"m":2,"i":2,"n":2,"h":2,"e":2,"l":5,"o":3,"w":1,"r":1}
posted @ 2019-08-15 20:09  des雷锋  阅读(...)  评论(...编辑  收藏