Skip to main content

$map

The $map operator transforms each element in an array using a provided transformation.

Input

  • An array containing an array to map and a transformation to apply to each element

Output

  • A new array containing the transformed elements

Examples

Map Numbers to Doubles

{
  "$map": [
    [1, 2, 3, 4, 5],
    {
      "$multiply": [
        {
          "$var": ["current"]
        },
        2
      ]
    }
  ]
}
Result: [2, 4, 6, 8, 10]

Transform Objects

{
  "$map": [
    [
      {"name": "Alice", "age": 25},
      {"name": "Bob", "age": 30}
    ],
    {
      "$concat": [
        {
          "$var": ["current"]
        },
        {
          "$input": "name"
        },
        " is ",
        {
          "$toString": [
            {
              "$var": ["current"]
            },
            {
              "$input": "age"
            }
          ]
        },
        " years old"
      ]
    }
  ]
}
Result: ["Alice is 25 years old", "Bob is 30 years old"]