方法一: let names = [1,3,3]; let length = names.length / 2 let nameNum = names.reduce((pre,cur)=>{ ? if(cur in pre){ ? ? pre[cur]++ ? }else{ ? ? pre[cur] = 1 ? } ? return pre },{}) console.log(nameNum) let obj = Object.entries(nameNum) console.log(obj) let add = obj.filter((item) => { ? ? return item[1]>length }) let kkk = add[0] || 0 let ooo = add.length>0 ? kkk[0]:0 console.log(ooo)
方法二: let names = [1,3,3]; let out = 0 let length = names.length / 2 let nameNum = names.reduce((pre,cur)=>{ ? if(cur in pre){ ? ? pre[cur]++ ? }else{ ? ? pre[cur] = 1 ? } ? return pre },{}) console.log(nameNum) for(let [key,value] of Object.entries(nameNum)) { ? ? if(value > length){ ? ? ? ? out = value ? ? } } console.log(out)
|