Skip to content

arrayReplaceAt

Description

Returns a new array with the element at the specified position replaced by a new value.

  • The element type must match the array type.
  • Supported element types include string, bool, number, interval, timestamp, regexp, and enum.

Syntax

Like many functions in DataPrime, arrayReplaceAt supports two notations, function and method notation. These interchangeable forms allow flexibility in how you structure expressions.

arrayReplaceAt(array: array<T>, position: number, value: T): array<T>
(array: array<T>).arrayReplaceAt(position: number, value: T): array<T>

Arguments

NameTypeRequiredDescription
arrayarraytrueThe array to modify
positionnumbertrueThe index of the element to replace (0-indexed)
valueTtrueThe replacement value, must match the array type

Example

Use case: Replace outdated schema values at a specific position

Suppose you have a list of values where the first entry uses an outdated schema. Consider the following inputs:

{
    "values": ["NewVal1", "NewVal2", "NewVal3"]
},
{
    "values": ["OldVal1", "NewVal2", "NewVal3"]
}

By replacing the element at position 0 with "NewVal1", you ensure consistent schema values across documents.

create updated_values from arrayReplaceAt(values, 0, 'NewVal1')
create updated_values from values.arrayReplaceAt(0, 'NewVal1')

Output

The result will replace the outdated value at the given index:

{
    "values": ["NewVal1", "NewVal2", "NewVal3"]
},
{
    "values": ["NewVal1", "NewVal2", "NewVal3"]
}