bmel:map¶
Category: Array & Statistics · Returns: bmel:array
Signature
bmel:map(array: bmel:array, body: bmel:expression)
Description¶
Evaluates bmel:body once per element of bmel:array and returns the results as a new array of the same length and order. Within bmel:body the current element is accessible as the special reference bmel:item and its zero-based position as bmel:i. Use it to compute a value per element before aggregating — e.g. the tokens of each invocation of a MANY operation: bmel:sum(bmel:map({op:Response Payload[]}.$.responseText, bmel:token-count(bmel:item, 'cl100k_base'))). Returns an empty array for an empty or null bmel:array.
Arguments¶
| Parameter | Type | Required | Description |
|---|---|---|---|
array | bmel:array | The array to transform — a payload array, or the N occurrences of a value of a MANY operation. | |
body | bmel:expression | Expression evaluated for each element. The current element is accessible as bmel:item and its zero-based position as bmel:i. |
Examples¶
Tokens of each answer of a run
bmel:map({createChatCompletion:Response Payload[]}.$.choices, bmel:token-count(bmel:item, 'cl100k_base'))
Line totals of an order
bmel:map({getOrder:Response Payload}.$.items, bmel:item.price * bmel:item.quantity)
Order total from its lines
bmel:sum(bmel:map({getOrder:Response Payload}.$.items, bmel:item.price * bmel:item.quantity))