# collections.Union
> 返回两个给定切片中的全部不重复元素。
- 官方英文原文：https://gohugo.io/functions/collections/union/
- 本页规范地址：https://hugozh.cn/functions/collections/union/
- 最近更新：2026-10-02
- 最后提交：912b1d3 chore(site): 添加 static/CNAME（hugozh.cn），供 GitHub Pages 等平台绑定自定义域名
- 签名：collections.Union SLICE1 SLICE2
- 返回类型：[]any
- 站点：Hugo 中文文档（https://hugozh.cn/）· 社区维护的非官方中文翻译，如有出入以官方英文原文为准

---
## 基本用法

用 `union` 函数合并两个切片，并去掉重复元素：

```go-html-template
{{ union (slice 1 2 3) (slice 3 4 5) }} → [1 2 3 4 5]
{{ union (slice 1 2 3) nil }}           → [1 2 3]
{{ union nil (slice 1 2 3) }}           → [1 2 3]
{{ union nil nil }}                     → []
```

## where 查询中的 OR 过滤

与 where 组合使用时，它也可以当作 `OR` 过滤器：

```go-html-template
{{ $pages := where .Site.RegularPages "Type" "not in" (slice "page" "about") }}
{{ $pages = $pages | union (where .Site.RegularPages "Params.pinned" true) }}
{{ $pages = $pages | intersect (where .Site.RegularPages "Params.images" "!=" nil) }}
```

上面的代码会取出类型不是 `page` 或 `about` 的普通页面，除非它们被 pin 了。最后，我们排除掉所有未在 Page 参数中设置 `images` 的页面。

`AND` 请参见 [intersect][]。

[intersect]: /functions/collections/intersect/

