1. 获取两个数组中某个属性值相等的项
let a=[{name:1},{name:4},{name:3}]
let b=[{name:5},{name:4},{name:2}]
let index = a.findIndex(aItem => b.some(bItem => bItem.name === aItem.name))
console.log(index, a[index]) // 1, {name:4}
2.筛选获取两个数组的共同项
let arr = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, { id: 5 }]
let brr = [3, 4, 5]
let list = arr.filter(item => brr.indexOf(item.id) > -1)
console.log('list ', list )
手机扫一扫
移动阅读更方便
你可能感兴趣的文章