collections.Complement
Syntax
collections.Complement COLLECTION [COLLECTION...]
Returns
any
Alias
complement
要查找 $c3
中不存在于 $c1
或 $c2
中的元素:
{{ $c1 := slice 3 }}
{{ $c2 := slice 4 5 }}
{{ $c3 := slice 1 2 3 4 5 }}
{{ complement $c1 $c2 $c3 }} → [1 2]
{{ $c3 | complement $c1 $c2 }} → [1 2]
您还可以在页面集合中使用 complement
函数。假设您的网站具有五种内容类型:
content/
├── blog/
├── books/
├── faqs/
├── films/
└── songs/
要列出除博客文章(blog
)和常见问题解答(faqs
)之外的所有内容:
{{ $blog := where site.RegularPages "Type" "blog" }}
{{ $faqs := where site.RegularPages "Type" "faqs" }}
{{ range site.RegularPages | complement $blog $faqs }}
<a href="{{ .RelPermalink }}">{{ .Title }}</a>
{{ end }}
{{ range where site.RegularPages "Type" "not in" (slice "blog" "faqs") }}
<a href="{{ .RelPermalink }}">{{ .Title }}</a>
{{ end }}
在此示例中,我们使用 complement
函数从句子中删除停用词:
{{ $text := "The quick brown fox jumps over the lazy dog" }}
{{ $stopWords := slice "a" "an" "in" "over" "the" "under" }}
{{ $filtered := split $text " " | complement $stopWords }}
{{ delimit $filtered " " }} → The quick brown fox jumps lazy dog