| 1 |
--- |
| 2 |
title: collections.Intersect |
| 3 |
description: Returns a slice containing the common elements found in two given slices, in the same order as the first slice. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [intersect] |
| 9 |
returnType: '[]any' |
| 10 |
signatures: [collections.Intersect SLICE1 SLICE2] |
| 11 |
aliases: [/functions/intersect] |
| 12 |
--- |
| 13 |
|
| 14 |
A useful example is to use it as `AND` filters when combined with where: |
| 15 |
|
| 16 |
```go-html-template |
| 17 |
{{ $pages := where .Site.RegularPages "Type" "not in" (slice "page" "about") }} |
| 18 |
{{ $pages := $pages | union (where .Site.RegularPages "Params.pinned" true) }} |
| 19 |
{{ $pages := $pages | intersect (where .Site.RegularPages "Params.images" "!=" nil) }} |
| 20 |
``` |
| 21 |
|
| 22 |
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. |
| 23 |
|
| 24 |
See [union][] for `OR`. |
| 25 |
|
| 26 |
[union]: /functions/collections/union/ |