每日搞点事之(数组并集交集差集操作)

数据,求差集:

let arr = ["5d92e755f55fb69c4d1412f439924575", "02845074a05b3c8fca8afdb0f9cd4f50"]
let arr2 = [
    {id: "5d92e755f55fb69c4d1412f439924575", nickName: "黄六", telphone: "13277777777"},
    {id: "02845074a05b3c8fca8afdb0f9cd4f50", nickName: "李四", telphone: "13211111111"},
    {id: "9446bf378f30900f0ca1a40b76879ee9", nickName: "dd", telphone: "13632266666"},
    {id: "4fbe47c8cbbf100cd90cc08e7dd3f620", nickName: "王五", telphone: "13244444444"},
    {id: "547412496736bd07ec4f9924312c393c", nickName: "张三", telphone: "13222222222"},
]

解:

// filter includes // 最简单
arr2.filter( item => !arr.includes(item.id) )
// filter some
arr2.filter( item => !arr.some((id => id == item.id) ))
// filter find
arr2.filter( item => !arr.find((id => id == item.id) ))
// filter indexOf
arr2.filter( item => arr.indexOf(item.id) == -1)
// set
arr2.filter(item => !new Set(arr).has(item.id))

注:

  • 并集使用[…a,…b]
  • 交集把!去掉即是

你可能感兴趣的:(前端原理,es6)