Design

Filtering arrays

Filter your array
COMMUNITY FEATURE Available for:
Carbone Cloud
Carbone On-premise
Embedded Carbone JS
  v2.0+ 

Tips: To hide a row in a table with a complex condition, using a Smart Conditional Block is often easier to manage. See the simple example below.

New syntax in v5+

NEW v5.0.0+

Prior to Carbone v5, filters needed to be repeated on all [i] tags, like in most example on this page.

With v5 and later, a single [i+1] tag containing the filter is sufficient. Repeating the filter is unnecessary. It works for all types of array filters described on this page.

Carbone v5 still supports the v4 syntax for backward compatibility, where the filter must be repeated on all tags.

data
[
  { "name": "John", "age": 20 },
  { "name": "Eva", "age": 18 },
  { "name": "Bob", "age": 25 },
  { "name": "Charly", "age": 30 }
]
template
People{d[i].name} {d[i].age}{d[i+1, age > 19, age < 30].name}
Carbone Merge Icon
result
PeopleJohn 20Bob 25

Number filters

You can use conditional operators to filter arrays: > , < , >= , <= , =, !=.

Since Carbone v5, filters must be the same on all tags if you want to keep only mat ching rows. Otherwise, only the tag is hidden, not the entire row.

Use Alias to simplify report maintenance and avoid repetitions of code.

data
[
  { "name": "John", "age": 20 },
  { "name": "Eva", "age": 18 },
  { "name": "Bob", "age": 25 },
  { "name": "Charly", "age": 30 }
]
template
People{d[i, age > 19, age < 30].name}{d[i+1, age > 19, age < 30 ].name}
Carbone Merge Icon
result
PeopleJohnBob

You can add multiple filters by separating them with commas. Each comma represents an AND operator, so all conditions must be met for the filter to apply. If you need to use an OR operator, see the advanced filter example.

String filters

String values can also be used for filtering, as shown in the following example. Both = and != operators are supported.

data
[
  { "name": "Falcon 9", "type": "rocket" },
  { "name": "Model S", "type": "car" },
  { "name": "Model 3", "type": "car" },
  { "name": "Falcon Heavy", "type": "rocket" }
]
template
People{d[i , type='rocket'].name}{d[i+1, type='rocket'].name}
Carbone Merge Icon
result
PeopleFalcon 9Falcon Heavy

Filter the first N items

Filtering the loop index is also possible as the following example:

data
[
  { "name": "Falcon 9" },
  { "name": "Model S" },
  { "name": "Model 3" },
  { "name": "Falcon Heavy" }
]
template
People{d[i, i < 2].name}{d[i+1, i < 2].name}
Carbone Merge Icon
result
PeopleFalcon 9Model S

Get inspired by one of our real-life examples: Best Manufacturer Awards

Exclude the last N items

Negative loop index i is accepted to print all elements except the last N-th items:

data
[
  { "name": "Falcon 9" },
  { "name": "Model S" },
  { "name": "Model 3" },
  { "name": "Falcon Heavy" }
]
template
The last item{d[i=-1].name} Exclude the last itemPeople{d[i, i!=-1].name}{d[i+1, i!=-1].name}Exclude the last two itemsPeople{d[i, i<-2].name}{d[i+1, i<-2].name}
Carbone Merge Icon
result
The last itemFalcon Heavy Exclude the last itemPeopleFalcon 9Model SModel 3Exclude the last two itemsPeopleFalcon 9Model S

Advanced filter

NEW v5.0.0+

When your filter is complex (for example, if you need "OR" logic, or mathematical calculations), you can first store an intermediate result using the set formatter.

In the example below, we want to keep only the rows where the name contains either "3" or "9". The set formatter is used to create a new attribute called .isShown in the JSON, which indicates whether each item matches the complex condition:

{d[].name:ifIN(3):or:ifIN(9):show(1):elseShow(0):set(.isShown)}

Then, you can use this new attribute as a filter for the array.

data
[
  { "name": "Falcon 9" },
  { "name": "Model S" },
  { "name": "Model 3" },
  { "name": "Falcon Heavy" }
]
template
{d[].name:ifIN(3):or:ifIN(9):show(1):elseShow(0):set(.isShown)}People{d[i].name}{d[i+1, isShown=1].name}
Carbone Merge Icon
result
PeopleFalcon 9Model 3

Smart filter

Sometimes, it is easier to use Smart Conditional Block to hide a row in a table:

You can use complex conditions with formatters.

data
[
  { "name": "Falcon 9" },
  { "name": "Model S" },
  { "name": "Model 3" },
  { "name": "Falcon Heavy" }
]
template
People{d[i].name}
{d[i].name:ifIN('Falcon'):drop(row)}
{d[i+1].name}
Carbone Merge Icon
result
PeopleModel S
Model 3