js 判断数组里面的对象有多少个相等

2020-04-01  本文已影响0人  yuki20

constarr= [

{name:"2",age:18},

{name:"23",age:19},

{name:"2",age:19}

    ];

functiongroup(arr) {

varobj= {};

if(Array.isArray(arr)) {

for(vari=0;i

varname=arr[i].name;

if(nameinobj)obj[name].push(arr[i]);

elseobj[name] = [arr[i]];

varage=arr[i].age;

if(ageinobj)obj[age].push(arr[i]);

elseobj[age] = [arr[i]];

        }

      }

returnobj;

    }

functionmax(obj) {

varret=0;

if(obj&&typeofobj==="object") {

for(varkeyinobj) {

varlength=obj[key].length;

if(length>ret)ret=length;

        }

      }

returnret;

    }

console.log(max(group(arr)));  //  2

上一篇 下一篇

猜你喜欢

热点阅读