# intersectionWith

# Description

这个方法类似 intersection,区别是它接受一个 comparator 调用比较 arrays 中的元素。结果值是从第一数组中选择。comparator 会传入两个参数:(arrVal, othVal)。

# Params

arrays

最后一个参数作为 comparator

# Return

Array

# Depend

import map from './map.js'
import baseIntersection from './.internal/baseIntersection.js'
import castArrayLikeObject from './.internal/castArrayLikeObject.js'
import last from './last.js'

map 源码分析

baseIntersection 源码分析

castArrayLikeObject 源码分析

last 源码分析

# Code

function intersectionWith(...arrays) {
  let comparator = last(arrays)
  const mapped = map(arrays, castArrayLikeObject)

  comparator = typeof comparator === 'function' ? comparator : undefined
  if (comparator) {
    mapped.pop()
  }
  return (mapped.length && mapped[0] === arrays[0])
    ? baseIntersection(mapped, undefined, comparator)
    : []
}

# Analyze

intersectionBy 基本一致,只不过这里对于 comparator 的合法性做了校验

# Example

console.log(intersectionWith([1,2,3,4,5], [2,3,4,5,6], 5)) // []