Transformation functions
mapEach
Commentary
added in 0.7.16
Applies function
to each element in the array over
.
Optionally, specify juxt
to juxtapose a series of key paths before function
is applied. 1
Examples
Applying a function
Use function
to apply a function to each element in the array. To access the element, reference the reserved variable mapItem
. This variable is only allowed in the context of a mapped function.
{
"_gen": "mapEach",
"over": [
0,
0.25,
0.5,
0.75,
1
],
"function": {
"_gen": "sin",
"degrees": {
"_gen": "var",
"var": "mapItem"
}
}
}
[
[
0,
0.004363309284746571,
0.008726535498373935,
0.013089595571344441,
0.01745240643728351
]
]
Juxtaposing paths
You can optionally specify juxt
, which is an array of paths. This is useful for picking apart composite objects into an array of values, and then applying function
to the resulting array.
This example sums the total value of a shopping cart, where the cart contains items with prices and quantities.
juxt
extracts the price and quantity in each item, applies the multiply
function, then passes the resulting array to add
to sum up the final value.
{
"_gen": "add",
"args": {
"_gen": "mapEach",
"over": {
"_gen": "repeatedly",
"n": 3,
"target": {
"price": {
"_gen": "uniformDistribution",
"bounds": [
5,
16
],
"decimals": 2
},
"quantity": {
"_gen": "uniformDistribution",
"bounds": [
1,
5
],
"decimals": 0
}
}
},
"juxt": [
[
"price"
],
[
"quantity"
]
],
"function": {
"_gen": "multiply",
"args": {
"_gen": "var",
"var": "mapItem"
}
}
}
}
[
145.18,
90.96000000000001,
42.07,
54.9,
142.85999999999999
]
Specification
JSON schema
{
"type": "object",
"properties": {
"over": {
"oneOf": [
{
"type": "array"
},
{
"type": "object",
"properties": {
"_gen": {
"type": "string"
}
},
"required": [
"_gen"
]
}
]
},
"juxt": {
"type": "array",
"items": {
"type": "array",
"items": {
"oneOf": [
{
"type": "integer",
"minimum": 0
},
{
"type": "string"
}
]
}
}
},
"function": {
"type": "object",
"properties": {
"_gen": {
"type": "string"
}
},
"required": [
"_gen"
]
}
},
"required": [
"over",
"function"
]
}