mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-31 15:27:50 +00:00
33 lines
1.2 KiB
JavaScript
33 lines
1.2 KiB
JavaScript
import baseFlatten from './.internal/baseFlatten.js'
|
|
import baseUniq from './.internal/baseUniq.js'
|
|
import isArrayLikeObject from './isArrayLikeObject.js'
|
|
import last from './last.js'
|
|
|
|
/**
|
|
* This method is like `union` except that it accepts `comparator` which
|
|
* is invoked to compare elements of `arrays`. Result values are chosen from
|
|
* the first array in which the value occurs. The comparator is invoked
|
|
* with two arguments: (arrVal, othVal).
|
|
*
|
|
* @since 4.0.0
|
|
* @category Array
|
|
* @param {...Array} [arrays] The arrays to inspect.
|
|
* @param {Function} [comparator] The comparator invoked per element.
|
|
* @returns {Array} Returns the new array of combined values.
|
|
* @see difference, union, unionBy, without, xor, xorBy
|
|
* @example
|
|
*
|
|
* const objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]
|
|
* const others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }]
|
|
*
|
|
* unionWith(objects, others, isEqual)
|
|
* // => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }]
|
|
*/
|
|
function unionWith(...arrays) {
|
|
let comparator = last(arrays)
|
|
comparator = typeof comparator == 'function' ? comparator : undefined
|
|
return baseUniq(baseFlatten(arrays, 1, isArrayLikeObject, true), undefined, comparator)
|
|
}
|
|
|
|
export default unionWith
|