# \_.unionWith( arrays , comparator )

URL: https://caijiao.org/lodash/docs/Array/_.unionWith
Source: docs/lodash/docs/Array/_.unionWith.md
Description: _.unionWith([arrays], [comparator])用于创建一个包含所有传入数组中的唯一元素的新数组，使用自定义比较函数 comparator 来确定元素的唯一性。

`_.unionWith([arrays], [comparator])` 是 [Lodash](/lodash/) 库提供的 JavaScript 方法。它用于创建一个包含所有传入数组中的唯一元素的新数组，使用自定义比较函数 `comparator` 来确定元素的唯一性。

它的工作方式如下：

- `arrays`：包含多个数组的数组。
- `comparator`（可选）：用于确定元素唯一性的自定义比较函数。

应用举例：

```javascript
// 原始数组
const array1 = [{ x: 1, y: 2 }];
const array2 = [{ x: 2, y: 1 }];
const array3 = [{ x: 1, y: 2 }];

// 自定义比较函数，比较对象的 'x' 和 'y' 属性是否相等
const comparator = (a, b) => a.x === b.x && a.y === b.y;

// 创建包含所有唯一元素的新数组，使用自定义比较函数来确定唯一性
const unionArray = _.unionWith([array1, array2, array3], comparator);

console.log("合并后的数组:", unionArray); // 输出合并后的数组: [ { x: 1, y: 2 }, { x: 2, y: 1 } ]
```

在这个例子中，`_.unionWith` 方法将多个数组合并成一个新数组，并使用自定义比较函数来确定元素的唯一性。
