sf-utils2 sf-utils2
版本v3.3.3-beta1
首页
  • 01.快速开始 🔥
  • 02.基础-Base
  • 03.对象-Object
  • 04.数组-Array
  • 05.方法-Function
  • 06.字符串-String
  • 07.数学-Math
  • 08.dom
  • 09.拓展
  • webpack5.x教程学习 (opens new window)
  • 例子
  • 教程 🔥
  • 例子配置
企业级后台模版 (opens new window)
版本v3.3.3-beta1
首页
  • 01.快速开始 🔥
  • 02.基础-Base
  • 03.对象-Object
  • 04.数组-Array
  • 05.方法-Function
  • 06.字符串-String
  • 07.数学-Math
  • 08.dom
  • 09.拓展
  • webpack5.x教程学习 (opens new window)
  • 例子
  • 教程 🔥
  • 例子配置
企业级后台模版 (opens new window)
  • 快速开始

  • 基础-Base

  • 对象-Object

  • 数组-Array✨✨✨

    • 序言 👏
    • uniq 【数组去重】
    • arrayToMap 【数组转成Map】
    • arrayToObj 【数组转成obj】
    • chunk 【数组切割】
    • compact 【去除数组中无效值】
    • remove 【数组中移除一个元素】
    • shuffle 【数组随机打乱】
    • groupBy 【分组】
    • compactIsNoNullable 【去除数组中null/undefined/NaN】
    • arrayDiff【比较数组差异】🔥🔥
    • differenceBy【获取新增数组】🔥
    • intersection 【数组交集】
      • 1.示例
      • 2.入参说明
      • 3.源码
    • matchSubListGroup
    • listToTree【列表转树形】🔥🔥🔥
    • treeToList【树型转列表】🔥🔥🔥
    • eachTree【遍历树形结构】🔥🔥🔥
    • filterTree【过滤树形结构】🔥🔥🔥
    • childNodesInList【列表中获取某个下所有后代节点】🔥
    • parentNodesInTree【tree中获取所有祖先节点】 🔥
    • formatStrategyIdsInTree【树形数据中只选中父节点】 🔥
    • range 【序号列表生成函数】
  • 方法-Function

  • 字符串-String

  • 数学-Math

  • 文件-Buffer

  • 节点-dom

  • 拓展

  • nodejs

目录

intersection 【数组交集】

描述

得到多个数组的交集 v3.0.9+

# 1.示例

import { intersection } from 'sf-utils2'

const a1 = [
 { empId: 1, empName: '蔡徐坤' },
 { empId: 2, empName: '小明' },
 { empId: 3, empName: '小红' },
]

const a2 = [
 { empId: 1, empName: '蔡徐坤' },
 { empId: 4, empName: '小明4' },
 { empId: 5, empName: '小红5' },
]

// 根据属性名转化
intersection([a1, a2], 'empId')
// 输出
[{empId: 1, empName: '蔡徐坤'}]

// 不传属性名
const a1 = [1, 2, 3, '4']
const a2 = [1, 2, 4, 5]
intersection([a1, a2])
// 输出
[1, 2]


intersection(
  [
     [1, 2, '3', 4],
     [1, 2, 3, '4', [1, '4']],
     [1, 2, '4']
  ]
)
=>
[1, 2]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

# 2.入参说明

参数 说明 类型 是否必填
val 值 Array --------

# 3.源码

源码,点开查看 👈
import arrayToMap from '@/array/arrayToMap'
import uniq from '@/array/uniq'
import isPrimitive from '@/base/isPrimitive'

/**
 * 获取多个数组的交集
 * @param {Array[]} array
 * @param {String|unknown} property
 * @return {[string, any]|*[]}
 */
function intersection(array, property) {
  const arrayFlatten = array.flat(1) || []
  console.log('array', arrayFlatten)
  const arrayMap$property = arrayToMap(arrayFlatten, property, { valueType: 'array' })
  const [array$0] = arrayFlatten
  const result = []
  arrayMap$property.forEach((v, k) => {
    if (v?.length === array.length) {
      result.push(...v)
    }
  })
  if (isPrimitive(array$0)) {
    return uniq(result)
  }
  return result
}

export default intersection
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
上次更新: 2023/06/24, 19:35:48
differenceBy【获取新增数组】🔥
matchSubListGroup

← differenceBy【获取新增数组】🔥 matchSubListGroup→

Theme by Vdoing | Copyright © 2022-2025 bianpengfei
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×