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, andenum.
Syntax
Like many functions in DataPrime, arrayReplaceAt supports two notations, function and method. These interchangeable forms allow flexibility in how you structure expressions.
- Function notation
- Method notation
arrayReplaceAt(array: array<T>, position: number, value: T): array<T>
(array: array<T>).arrayReplaceAt(position: number, value: T): array<T>
Arguments
| Name | Type | Required | Description |
|---|---|---|---|
array | array<T> | true | The array to modify |
position | number | true | The index of the element to replace (0-indexed) |
value | T | true | The 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.
Example query
- Function notation
- Method notation
create updated_values from arrayReplaceAt(values, 0, 'NewVal1')
create updated_values from values.arrayReplaceAt(0, 'NewVal1')
Example output
The result will replace the outdated value at the given index:
{
"values": ["NewVal1", "NewVal2", "NewVal3"]
},
{
"values": ["NewVal1", "NewVal2", "NewVal3"]
}