Skip to content

set

The set() function replaces the value at a given index and returns a new array without modifying the original.

Interactive example

Syntax

Classic signature

typescript
function set<
	GenericElement extends unknown
>(
	input: readonly GenericElement[],
	index: number, 
	element: GenericElement
): GenericElement[];

Curried signature

typescript
function set<
	GenericElement extends unknown
>(
	index: number, 
	element: GenericElement
): (input: readonly GenericElement[]) => GenericElement[];

Parameters

  • input: The source array.
  • index: The index to replace (indexes are normalized with a modulo on length, so out-of-range values target an existing position).
  • element: The new value.

Return value

A new array whose targeted element (after index normalization) matches element. Other elements are copied as is.

See also

  • fill - Fills a portion of the array
  • copyWithin - Copies a segment within the same array

Sources

Released under the MIT license.