Debian Codegraph @

docs/content/en/functions/collections/Union.md

raw
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 ## Basic usage
15
16 Use the `union` function to combine two slices, removing duplicate elements:
17
18 ```go-html-template
19 {{ union (slice 1 2 3) (slice 3 4 5) }} → [1 2 3 4 5]
20 {{ union (slice 1 2 3) nil }} → [1 2 3]
21 {{ union nil (slice 1 2 3) }} → [1 2 3]
22 {{ union nil nil }} → []
23 ```
24
25 ## OR filter in where query
26
27 This is also useful as `OR` filters when combined with where:
28
29 ```go-html-template
30 {{ $pages := where .Site.RegularPages "Type" "not in" (slice "page" "about") }}
31 {{ $pages = $pages | union (where .Site.RegularPages "Params.pinned" true) }}
32 {{ $pages = $pages | intersect (where .Site.RegularPages "Params.images" "!=" nil) }}
33 ```
34
35 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.
36
37 See [intersect][] for `AND`.
38
39 [intersect]: /functions/collections/intersect/