var arr = [
{ type: 1, age: 12, name: 'xiaohua'
},
{ type: 1, age: 12, name: 'xiaoming'
},
{ type: 1, age: 12, name: 'xiaohong'
},
{ type: 2, age: 14, name: 'xiaoxiao'
},
]在一个数组内,如何判断type和age 相等的项
2 回答
米琪卡哇伊
TA贡献1998条经验 获得超6个赞
let arr = [ { type: 1, age: 12, name: 'xiaohua' }, { type: 1, age: 12, name: 'xiaoming' }, { type: 1, age: 12, name: 'xiaohong' }, { type: 2, age: 14, name: 'xiaoxiao' }, { type: 11, age: 11, name: 'xixi' }, ] const res = arr.filter(ele=>ele.type===ele.age); console.log(res);
这样?
繁星点点滴滴
TA贡献1803条经验 获得超3个赞
// 生成二级映射var map = arr.reduce((p, c) => [ p[c.type] = p[c.type] || {}, p[c.type][c.age] = p[c.type][c.age] || [], p[c.type][c.age].push(c), p][3], {})// 根据映射的节点信息获取筛选后的数组列表 Object.keys(map).forEach(key => { Object.keys(map[key]).forEach(sKey => map[key][sKey].forEach(i => console.log(i)) ) console.log("================") })// {type: 1, age: 12, name: "xiaohua"}// {type: 1, age: 12, name: "xiaoming"}// {type: 1, age: 12, name: "xiaohong"}// ================// {type: 2, age: 14, name: "xiaoxiao"}// ================
- 2 回答
- 0 关注
- 89 浏览
添加回答
举报
0/150
提交
取消