# MAP Transforms each element of an array by applying a function or expression. ## Syntax ```basic 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 ```basic users = FIND "users", "status=active" names = MAP(users, "name") TALK "Active users: " + JOIN(names, ", ") ``` ### Transform Values ```basic 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 ```basic orders = FIND "orders", "date=today" summaries = MAP(orders, "'Order #' + item.id + ': $' + item.total") FOR EACH summary IN summaries TALK summary NEXT ``` ### Extract Nested Properties ```basic contacts = FIND "contacts", "company=Acme" emails = MAP(contacts, "email") email_list = JOIN(emails, "; ") TALK "Emails: " + email_list ``` ### Uppercase Names ```basic 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 ```basic records = FIND "items", "sync=pending" ids = MAP(records, "id") ' Use ids for batch API operations ``` ### Create Display Labels ```basic products = FIND "products", "in_stock=true" labels = MAP(products, "item.name + ' ($' + item.price + ')'") ``` ### Calculate Derived Values ```basic line_items = FIND "cart_items", "cart_id=123" totals = MAP(line_items, "item.quantity * item.unit_price") ``` ## See Also - [FILTER](./keyword-filter.md) - Filter array elements - [FOR EACH](./keyword-for-each.md) - Iterate with more control - [JOIN](./keyword-join.md) - Combine mapped results into string - [AGGREGATE](./keyword-aggregate.md) - Calculate summary from mapped values ---