Skip to content

map

The map() method applies a transform function to each element of an array and returns a new array containing the results, without modifying the input.

Interactive example

Syntax

Classic signature

typescript
function map<
	GenericInput extends readonly unknown[],
	GenericOutput extends unknown
>(
	input: GenericInput,
	theFunction: (
		element: GenericInput[number],
		params: ArrayMapParams<GenericInput>
	) => GenericOutput
): GenericOutput[]

Curried signature

typescript
function map<
	GenericInput extends readonly unknown[],
	GenericOutput extends unknown
>(
	theFunction: (
		element: GenericInput[number],
		params: ArrayMapParams<GenericInput>
	) => GenericOutput
): (input: GenericInput) => GenericOutput[]

Helper parameters

typescript
interface ArrayMapParams<
	GenericInputArray extends readonly unknown[]
> {
	index: number;
	self: GenericInputArray;
}

Parameters

  • input: The array to transform.
  • theFunction: Function applied to each element. It receives the current element and an object providing the index and the full array.
  • params.index: Position of the current element in the source array.
  • params.self: The full array (handy for comparing the current position or accessing neighbors).

Return value

A new array containing the values returned by the transform function. The original array is never modified.

See also

  • filter - Filters elements according to a condition
  • flatMap - Transforms then flattens the result by one level

Sources

Released under the MIT license.