| 1 |
--- |
| 2 |
title: collections.Union |
| 3 |
description: Returns a slice containing the unique elements from two given slices. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [union] |
| 9 |
returnType: '[]any' |
| 10 |
signatures: [collections.Union SLICE1 SLICE2] |
| 11 |
aliases: [/functions/union] |
| 12 |
--- |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
```go-html-template |
| 17 |
{{ union (slice 1 2 3) (slice 3 4 5) }} → [1 2 3 4 5] |
| 18 |
{{ union (slice 1 2 3) nil }} → [1 2 3] |
| 19 |
{{ union nil (slice 1 2 3) }} → [1 2 3] |
| 20 |
{{ union nil nil }} → [] |
| 21 |
``` |
| 22 |
|
| 23 |
## OR filter in where query |
| 24 |
|
| 25 |
This is also useful as `OR` filters when combined with where: |
| 26 |
|
| 27 |
```go-html-template |
| 28 |
{{ $pages := where .Site.RegularPages "Type" "not in" (slice "page" "about") }} |
| 29 |
{{ $pages = $pages | union (where .Site.RegularPages "Params.pinned" true) }} |
| 30 |
{{ $pages = $pages | intersect (where .Site.RegularPages "Params.images" "!=" nil) }} |
| 31 |
``` |
| 32 |
|
| 33 |
The above fetches regular pages not of `page` or `about` type unless they are pinned. And finally, we exclude all pages with no `images` set in Page parameters. |
| 34 |
|
| 35 |
See [intersect][] for `AND`. |
| 36 |
|
| 37 |
[intersect]: /functions/collections/intersect/ |