Skip to content

transform

Signature

js
transform (obj, options)

Description

Applies a pipeline of transformations to an object or array. Operations are applied in the following order: toArraytoObjectsfiltermappingunitMappingpick / omit / merge → output normalisation.

By default, transformations are applied in place — the original input is mutated.

Parameters

NameTypeRequiredDescription
objobject | arrayyesThe input to transform
optionsobjectyesTransformation options
options.toArraybooleannoConvert a plain object to an array of its values
options.toObjectsstring[]noConvert an array of arrays to an array of objects using the given keys
options.filterobjectnoA sift query to filter array elements
options.inPlacebooleannoWhether to mutate the original input. Defaults to true
options.mappingobjectnoRename or remap keys — see mapping
options.unitMappingobjectnoConvert values — see unitMapping
options.pickstring[]noKeep only the listed properties
options.omitstring[]noRemove the listed properties
options.mergeobjectnoMerge additional properties into each element
options.asArraybooleannoReturn result as an array even if input was a plain object
options.asObjectbooleannoReturn the first element when input is an array

Returns

TypeDescription
object | arrayThe transformed result. Type matches the input unless asArray or asObject is set

Throws

Throws a TypeError if obj is not a plain object or array, or if options is not a plain object.


mapping

Renames or remaps keys. Each entry maps an input path to an output path.

js
options.mapping = {
  [inputPath]: outputPath | { path, values?, delete? }
}
PropertyTypeDescription
outputPathstringTarget key path
pathstringTarget key path (object form)
valuesobjectMap input values to output values
deletebooleanWhether to delete the input key after mapping. Defaults to true

unitMapping

Converts values at given paths. Each entry maps a path to a conversion descriptor.

js
options.unitMapping = {
  [path]: { asNumber?, asString?, asDate?, asCase?, from?, to?, empty? }
}
PropertyTypeDescription
asNumberbooleanConvert value to a number
asStringnumber | booleanConvert value to a string. Pass a radix number for base conversion (e.g. 16 for hex)
asDate'utc' | booleanParse value as a date. Use 'utc' for UTC parsing
fromstringInput format (moment format string) or source unit for physical conversion
tostringOutput format (moment format string) or target unit for physical conversion
asCasestringApply a lodash case function after conversion (e.g. 'camelCase', 'snakeCase', 'kebabCase')
empty*Fallback value to set if the path is missing from the object

Examples

js
// Rename a key
transform({ a: 1 }, { mapping: { a: 'b' } })
// { b: 1 }
js
// Map values
transform({ status: 1 }, {
  mapping: { status: { path: 'label', values: { 1: 'active', 0: 'inactive' } } }
})
// { label: 'active' }
js
// Convert string to number
transform({ n: '42' }, { unitMapping: { n: { asNumber: true } } })
// { n: 42 }
js
// Convert number to hex string
transform({ n: 255 }, { unitMapping: { n: { asString: 16 } } })
// { n: 'ff' }
js
// Format a date
transform({ ts: '2024-01-15' }, {
  unitMapping: { ts: { asDate: 'utc', from: 'YYYY-MM-DD', to: 'DD/MM/YYYY' } }
})
// { ts: '15/01/2024' }
js
// Convert physical units
transform({ d: 1 }, { unitMapping: { d: { from: 'km', to: 'mile' } } })
// { d: 0.621371 }
js
// Apply case after conversion
transform({ label: 'hello world' }, { unitMapping: { label: { asString: true, asCase: 'camelCase' } } })
// { label: 'helloWorld' }
js
// Full pipeline
transform(
  [['Alice', 30], ['Bob', 17], ['Carol', 25]],
  {
    toObjects: ['name', 'age'],
    filter: { age: { $gte: 18 } },
    pick: ['name']
  }
)
// [{ name: 'Alice' }, { name: 'Carol' }]
js
// Immutable transform
const json = [{ a: 1 }]
transform(json, { mapping: { a: 'b' }, inPlace: false })
// json is unchanged: [{ a: 1 }]