Skip to main content

Array filters

formatList

Convert an array into a string formatted as a sentence.

Input

{{ ["England", "Scotland", "Wales"] | formatList }}

Output

England, Scotland and Wales

To format the list using a disjunction:

Input

{{ ["England", "Scotland", "Wales"] | formatList("disjunction") }}

Output

England, Scotland or Wales

includes

Tests whether an array includes an item given, and returns true or false.

Input

{{ ['Orange', 'Banana', 'Apple'] | includes('Orange') }}
{{ ['Orange', 'Banana', 'Apple'] | includes('Pear') }}

Output

true
false

isArray

Check a value is classified as an Array object.

Input

{{ ["england", "scotland", "wales"] | isArray }}
{{ "great britain" | isArray }}

Output

true
false

rejectFromArray

Reject items in an array that have a key with a given value.

Input

{{ [{
  name: "Sally Smith",
  role: "admin"
}, {
  name: "David Jones",
  role: "user"
}] | rejectFromArray("role", "admin") | dump }}

Output

Using Nunjucks’ built-in dump filter to render the result as a string:

[{
  name: "David Jones",
  role: "user"
}]

selectFromArray

Select items in an array that have a key with a given value.

Input

{{ [{
  name: "Sally Smith",
  role: "admin"
}, {
  name: "David Jones",
  role: "user"
}] | selectFromArray("role", "admin") | dump }}

Output

Using Nunjucks’ built-in dump filter to render the result as a string:

[{
  name: "Sally Smith",
  role: "admin"
}]

uniqueFromArray

Return unique items from an array.

Input

{{ ['Orange', 'Banana', 'Apple', 'Orange'] | uniqueFromArray }}

Output

[Orange, Banana, Apple]