Array Functions (MOPS)
$pivot()
Signature: $pivot(data, keyFields, pivotField, measures)
Converts row-based data into columnar format by grouping, pivoting, and applying aggregations. It uses % to concatenate multiple keyFields. If in case, invalid input is provided then it will return an empty array.
Parameters
| Parameter | Type | Description |
|---|---|---|
data |
object[] | Array of objects to pivot |
keyFields |
string|string[] | Grouping column(s) to preserve as rows |
pivotField |
string | Column whose values become new headers |
measures |
[string, ‘SUM’|‘AVERAGE’][] | Array of [field, aggregation] pairs |
Examples
// JSONata
$pivot(
[
{Region: "West", Product: "A", Sales: 100},
{Region: "West", Product: "A", Sales: 150},
{Region: "East", Product: "B", Sales: 200}
],
["Region", "Product"],
"Product",
[["Sales", "SUM"]]
)
// Result
[
{Region: "West", Product: "A", A: 250},
{Region: "East", Product: "B", B: 200}
]
$stdev()
Signature: $stdev(arr)
Calculates population standard deviation (σ) for an array of numbers using the “n” formula (divided by N).
Parameters
| Parameter | Type | Description |
|---|---|---|
arr |
number[] | Array of numeric values to analyze |
Examples
- Basic calculation:
$stdev([1, 2, 3, 4, 5]) => 1.4142135623730951 (Population σ for 1-5: √2 ≈ 1.4142) - Empty array handling:
$stdev([]) => NaN - Single value array:
$stdev([42]) => 0 (No variation when only 1 value exists)