Design

Array search

Search an item within an array without doing a loop
COMMUNITY FEATURE Available for:
Carbone Cloud
Carbone On-premise
Embedded Carbone JS
  v2.0+ 

Simple filters

Instead of using the reserved word i to access the i-th item of an array, Carbone accepts filters using attributes of objects.

If the filter returns many rows, Carbone keeps only the first occurrence.

Use single quotes to filter text that contains spaces.

data
[
  { "name": "Inception", "year": 2010 },
  { "name": "Matrix", "year": 1999 },
  { "name": "Back to the future", "year": 1985 }
]
template
The movie of 1999 was {d[year=1999].name}Back to the future: {d[name='Back to the future'].year}
Carbone Merge Icon
result
The movie of 1999 was MatrixBack to the future: 1985

Variable filter

UPDATED v4.2.0+

If the second operand starts with a period, Carbone considers that the operand is a variable coming from the JSON

data
  {
    "parent" : {
      "qty" : 2
    },
    "subArray" : [{
      "text" : 1000,
      "other" : 1,
      "sub" : {
        "b" : { "c" : 1 }
      }
    },{
      "text" : 2000,
      "other" : 1,
      "sub" : {
        "b" : { "c" : 2 }
      }
    }]
  }
template
{d.subArray[sub.b.c = .other].text}{d.subArray[sub.b.c = ..parent.qty].text}
Carbone Merge Icon
result
10002000

Multiple array filters

Multiple filters are accepted, even using a sub-object.

If the filter returns many rows, Carbone keeps only the first occurrence.

data
[
  { "name": "Interstellar"  , "year": 2014, "meta": { "type": "SF"    } },
  { "name": "Matrix"        , "year": 1999, "meta": { "type": "SF"    } },
  { "name": "The Green Mile", "year": 1999, "meta": { "type": "Drama" } }
]
template
{d[year=1999, meta.type='SF'].name}
Carbone Merge Icon
result
Matrix

Last element

Get the last element of a list with a negative index [i=-1]. If you want to get the second to last, you have to use [i=-2]. Here is an example:

This filter can be used also in a repetition loop.

data
[
  { "name": "Inception", "year": 2010 },
  { "name": "Matrix", "year": 1999 },
  { "name": "BTTF", "year": 1985 }
]
template
The oldest movie was {d[i=-1].name}.
Carbone Merge Icon
result
The oldest movie was BTTF.

Filter and print parent

Access properties of the parent object with two points .. (or more) when you need to print a parent property using filters in nested arrays:

data
{
  "country": "USA",
  "movies": [
    { "name": "Inception", "year": 2010 },
    { "name": "Matrix", "year": 1999 },
    { "name": "BTTF", "year": 1985 }
  ]
}
template
{d.movies[year=1999]..country}
Carbone Merge Icon
result
USA