Skip to main content

map

$map

Polymorphic map operator. Applies a processor to each element of an array, or to each value of a plain object. Returns a new collection of the same shape with transformed values.

  • Array input: maps over elements; undefined results from the processor appear as undefined in output.
  • Object input: maps over values; keys are always preserved in the output.
  • Non-collection input: throws a ConstraintError.

Note that null returned by the processor is the standard prune signal and will remove the element/entry from the output.

Parameters

  • processor (any valid processor spec, required): Applied to each element or value.

Example

// Uppercase every string in an array
new Schema('array').transformer({$map: '$uppercase'})

// Parse every element as a number
new Schema('array').transformer({$map: '$number'})

// Apply a pipeline to every value in an object
new Schema('object').transformer({$map: ['$trim', '$lowercase']})
// {name: ' Alice ', city: ' NYC '} → {name: 'alice', city: 'nyc'}

// Extract a nested field from each element
new Schema('array').transformer({$map: {$get: 'id'}})
// [{id: 1, name: 'a'}, {id: 2, name: 'b'}] → [1, 2]