1:数组对象中取出某属性值组合成新的纯数组
使用map
let obj=[{id:"1",name:"11"}, {id:"2",name:"11"}, {id:"3",name:"11"}, {id:"4",name:"11"}]
let obj2=[{id:"1"}, {id:"2"}, {id:"3"}, {id:"5"}]
a =obj.map(obj => {return obj.id}) 结果为[1,2,3,4]
2:取出两个数组中相同的元素
let add2=obj2.filter(item=>obj.some(ele=>ele.id===item.id))
结果为[ {id: "1"} ,{id: "2"}, {id: "3"}]
3:取出两个数组中不相同的元素
let add2=obj2.filter(item=>!obj.some(ele=>ele.id===item.id))
结果为[ {id: "5"}]
网友评论