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.
[
{ "name": "Inception", "year": 2010 },
{ "name": "Matrix", "year": 1999 },
{ "name": "Back to the future", "year": 1985 }
]
The movie of 1999 was {d[year=1999].name}Back to the future: {d[name='Back to the future'].year}
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
{
"parent" : {
"qty" : 2
},
"subArray" : [{
"text" : 1000,
"other" : 1,
"sub" : {
"b" : { "c" : 1 }
}
},{
"text" : 2000,
"other" : 1,
"sub" : {
"b" : { "c" : 2 }
}
}]
}
{d.subArray[sub.b.c = .other].text}{d.subArray[sub.b.c = ..parent.qty].text}
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.
[
{ "name": "Interstellar" , "year": 2014, "meta": { "type": "SF" } },
{ "name": "Matrix" , "year": 1999, "meta": { "type": "SF" } },
{ "name": "The Green Mile", "year": 1999, "meta": { "type": "Drama" } }
]
{d[year=1999, meta.type='SF'].name}
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.
[
{ "name": "Inception", "year": 2010 },
{ "name": "Matrix", "year": 1999 },
{ "name": "BTTF", "year": 1985 }
]
The oldest movie was {d[i=-1].name}.
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:
{
"country": "USA",
"movies": [
{ "name": "Inception", "year": 2010 },
{ "name": "Matrix", "year": 1999 },
{ "name": "BTTF", "year": 1985 }
]
}
{d.movies[year=1999]..country}
USA