JS判断数组的值出现的次数,以及去重

        var arr = ["曹阳","曹阳","曹阳","张三","张三","张三","张三","张三",];
        var temp = [];
        var obj = {};
        var newArr = [];
        for (var i = 0; i < arr.length; i++) {
            var dt = arr[i]; 
            if (obj[dt]) {  
                obj[dt]++;
            } else { 
                temp.push(dt);
                obj[dt] = 1;
            }
        }
        console.log(temp);
        for (const i in obj) {
            newArr.push({
                'name':i,
                'value':obj[i]
            })
        }
        console.log(newArr); 

 

posted @ 2019-03-26 17:13  Hekiss  阅读(1106)  评论(0编辑  收藏  举报