collections.Union
语法
collections.Union SET1 SET2
返回值
any
别名
union
给定两个数组(或切片)A 和 B,此函数将返回一个新数组,其中包含属于 A 或 B 或两者的元素或对象。
{{ union (slice 1 2 3) (slice 3 4 5) }}
<!-- returns [1 2 3 4 5] -->
{{ union (slice 1 2 3) nil }}
<!-- returns [1 2 3] -->
{{ union nil (slice 1 2 3) }}
<!-- returns [1 2 3] -->
{{ union nil nil }}
<!-- returns an error because both arrays/slices have to be of the same type -->
在 where 查询中使用 OR 过滤器
当与 where 结合使用时,这对于用作 OR
过滤器也非常有用
{{ $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
类型的常规页面,除非它们被固定。 最后,我们排除所有在页面参数中没有设置 images
的页面。
有关 AND
,请参阅 intersect。