问题描述:我有下面这样一个数组,数组里面每个对象有三个相同的key:name、age、hair。letnamelist=[{name:'mark',age:15,hair:'long'},{name:'tuwen',age:16,hair:'short'},{name:'xiaoming',age:16,hair:'short'},{name:'lilei',age:15,hair:'short'},{name:'hanmei',age:17,hair:'long'}]筛选条件:数组中age相同的对象分到一个数组里。期望结果:arr_1=[{name:'mark',age:15,hair:'long'},{name:'lilei',age:15,hair:'short'}]arr_2=[{name:'tuwen',age:16,hair:'short'},{name:'xiaoming',age:16,hair:'short'}]arr_3=[{name:'hanmei',age:17,hair:'long'}]
2 回答
慕妹3242003
TA贡献1824条经验 获得超6个赞
letres=[]while(nameList.length!==0){letlist=nameList.shift()letarr=[list]for(leti=0;iif(nameList[i].age===list.age){ arr=arr.concat(nameList.splice(i,1))i--}}res.push(arr)}
米脂
TA贡献1836条经验 获得超3个赞
我的思路是先使用map得到所有的ageletone=namelist.map(function(e){returne.age})然后去重newSet(one)然后用filter方法,得到你想要的各个数组集合
添加回答
举报
0/150
提交
取消