Skip to content

intersectionSet ​

数组交集:获取同时存在于数组a 和 数组b 的元素(取自a中的元素)

引入版本 ​

0.110.0

Demo ​

ts
import { intersectionSet } from '@wyfex/iutils'

// 基础类型交集
const a1 = [1, 2, 3, 4]
const b1 = [2, 4, 5]
console.log(intersectionSet(a1, b1)) // [2,4]

// 对象数组,按key求交集
const a2 = [
  { id: 1, name: 'A' },
  { id: 2, name: 'B' },
  { id: 3, name: 'C' }
]
const b2 = [
  { id: 2, name: 'B' },
  { id: 4, name: 'D' }
]
console.log(JSON.stringify(intersectionSet(a2, b2, 'id'), null, 2))
// [
//   {
//     "id": 2,
//     "name": "B"
//   }
// ]

基于 MIT 许可发布