$filter
$filter(array, function) -> arrayCompact type signature
<af>Source: JSONata
Documentation
Returns the input items whose callback result is truthy under JSONata boolean rules.
The callback receives the current value, index, and source array. $filter() keeps the original matching items rather than the callback result, and undefined input returns undefined. Unlike $first(), $pMap(), and $pLimit(), this implementation expects an array or sequence input rather than wrapping a scalar as a one-item sequence.
Examples
Filter for higher-cost lines
Input
This example uses the claims-lines example input. The expression filters the claim lines for higher-cost entries.
Example input
JSON
[
{
"claimId": "clm-2001",
"lineNumber": 1,
"serviceCode": "PROC-010",
"serviceDisplay": "office visit",
"units": 1,
"unitPrice": 125,
"allowedAmount": 110,
"modifiers": [
"MOD-A"
]
},
{
"claimId": "clm-2001",
"lineNumber": 2,
"serviceCode": "PROC-020",
"serviceDisplay": "imaging study",
"units": "1",
"unitPrice": 350,
"allowedAmount": 300,
"modifiers": [
"MOD-B",
"MOD-C"
]
},
{
"claimId": "clm-2001",
"lineNumber": 3,
"serviceCode": "PROC-030",
"serviceDisplay": "lab panel",
"units": 2,
"unitPrice": 40,
"allowedAmount": 70,
"modifiers": []
},
{
"claimId": "clm-2002",
"lineNumber": 1,
"serviceCode": "PROC-010",
"serviceDisplay": "office visit",
"units": 1,
"unitPrice": 125,
"allowedAmount": 115,
"modifiers": [
"MOD-A"
]
},
{
"claimId": "clm-2002",
"lineNumber": 2,
"serviceCode": "PROC-040",
"serviceDisplay": "counseling",
"units": 3,
"unitPrice": 60,
"allowedAmount": 150,
"modifiers": [
"MOD-D"
]
}
]
Expression
$filter($, function($l) { $l.allowedAmount >= 150 }).{ "claimId": claimId, "lineNumber": lineNumber, "allowedAmount": allowedAmount }
Result
[
{ "claimId": "clm-2001", "lineNumber": 2, "allowedAmount": 300 },
{ "claimId": "clm-2002", "lineNumber": 2, "allowedAmount": 150 }
]