JavaScript练习题 | 计算数组中元素重复次数

 

 题目连接:https://github.com/iszu/web-learning/blob/master/JavaScript%E7%BB%83%E4%B9%A0%E9%A2%98.md

 

 

        function countRepeat(arr) {
            var a = {};
            for (var i = 0; i < arr.length; i++) {
                var t = arr[i];
                console.log(t);
                console.log(a[t]);

                if (a[t]) {
                    a[t]++;
                } else {
                    a[t] = 1;
                }
            }
            for (var i in a) {
                console.log(i + ":" + a[i]);

            }
        }


        countRepeat([2, 9, 8, 4, 5, 3, 4, 8, 9, 9, 1, 0, 2, 1])

 

posted @ 2020-09-11 14:42  huluxin  阅读(218)  评论(0编辑  收藏  举报