2021-07-26js计算数组中某个元素重复出现的个数

 const list = [
                {
                    creator_id: '156',
                    operation_group: '经营一组',
                    work_logs: [
                        {
                            time: '2021-07-01',
                            num: 1,
                            color: 'blue',
                        },
                        {
                            time: '2021-04',
                            num: 0,
                            color: 'red',
                        },
                    ],
                },
                {
                    creator_id: '40',
                    operation_group: '经营一组',
                    work_logs: [
                        {
                            time: '2021-07-01',
                            num: 1,
                            color: 'red',
                        },
                        {
                            time: '2021-04',
                            num: 0,
                            color: 'red',
                        },
                    ],
                },
                {
                    creator_id: '16',
                    operation_group: '经营二组',
                    work_logs: [
                        {
                            time: '2021-03',
                            num: 1,
                            color: 'blue',
                        },
                        {
                            time: '2021-04',
                            num: 0,
                            color: 'red',
                        },
                    ],
                },
                {
                    creator_id: '25',
                    operation_group: '经营二组',
                    work_logs: [
                        {
                            time: null,
                            num: 0,
                            color: 'red',
                        },
                        {
                            time: '2021-03',
                            num: 0,
                            color: 'red',
                        },
                        {
                            time: '2021-04',
                            num: 0,
                            color: 'red',
                        },
                    ],
                },

                {
                    creator_id: '177',
                    operation_group: '经营二组',
                    work_logs: [
                        {
                            time: null,
                            num: 0,
                            color: 'red',
                        },
                        {
                            time: '2021-03',
                            num: 0,
                            color: 'red',
                        },
                        {
                            time: '2021-04',
                            num: 0,
                            color: 'red',
                        },
                    ],
                },
            ];
 console.log('listTotal', listTotal);
            const titleList = listTotal.map(item => {
                return item.time;
            });
            console.log('titleList-----', titleList);
            // 不用reduce时:
            function getRepeatNum(arr) {
                const obj = {};
                for (let i = 0, l = arr.length; i < l; i++) {
                    const item = arr[i];
                    obj[item] = obj[item] + 1 || 1;
                }
                return obj;
            } // 用reduce时: //reduce:计算数组元素相加后的总和
            // reduce方法接受两个参数,第一个是函数,第二个是初始值
            function getRepeatNum2(arr) {
                return arr.reduce((prev, next) => {
                    prev[next] = prev[next] + 1 || 1;
                    return prev;
                }, {});
            }
            console.log('getRepeatNum-----', getRepeatNum2(titleList));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值