MAP
Transforms each element of an array by applying a function or expression.
Syntax
result = MAP(array, expression)
result = MAP(array, field)
Parameters
| Parameter | Type | Description |
|---|---|---|
array | Array | The source array to transform |
expression | String | Expression to apply to each element, or field name to extract |
Description
MAP creates a new array by applying a transformation to each element of the input array. This is useful for extracting specific fields from objects, formatting data, or performing calculations on each item.
Examples
Extract Field from Objects
users = FIND "users", "status=active"
names = MAP(users, "name")
TALK "Active users: " + JOIN(names, ", ")
Transform Values
prices = [100, 200, 300, 400]
with_tax = MAP(prices, "item * 1.1")
FOR EACH price IN with_tax
TALK "Price with tax: $" + price
NEXT
Format Data
orders = FIND "orders", "date=today"
summaries = MAP(orders, "'Order #' + item.id + ': $' + item.total")
FOR EACH summary IN summaries
TALK summary
NEXT
Extract Nested Properties
contacts = FIND "contacts", "company=Acme"
emails = MAP(contacts, "email")
email_list = JOIN(emails, "; ")
TALK "Emails: " + email_list
Uppercase Names
products = ["widget", "gadget", "gizmo"]
upper_products = MAP(products, "UPPER(item)")
TALK JOIN(upper_products, ", ")
' Output: "WIDGET, GADGET, GIZMO"
Return Value
Returns a new array with the same length as the input, containing transformed values.
- Original array is not modified
- Null values in the source are preserved as null
- If transformation fails for an element, that element becomes null
Sample Conversation
Common Patterns
Extract IDs for API Calls
records = FIND "items", "sync=pending"
ids = MAP(records, "id")
' Use ids for batch API operations
Create Display Labels
products = FIND "products", "in_stock=true"
labels = MAP(products, "item.name + ' ($' + item.price + ')'")
Calculate Derived Values
line_items = FIND "cart_items", "cart_id=123"
totals = MAP(line_items, "item.quantity * item.unit_price")
See Also
- FILTER - Filter array elements
- FOR EACH - Iterate with more control
- JOIN - Combine mapped results into string
- AGGREGATE - Calculate summary from mapped values