Skip to content

arrayReplaceAt - Replaces element at specific position

arrayReplaceAt will replace an element at a specific position and return the modified array.

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
arrayarray of type TtrueT must be either string, bool, number, interval, timestamp, regexp or enum
positionnumbertrueThe index at which to replace the element in the array
valueTtrueT must be either string, bool, number, interval, timestamp, regexp or enum

Example - Cleaning up an old value in a list

Consider the following two documents:

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

We can see that there has been a schema change between NewVal1 and OldVal1. To overcome this, we can use arrayReplaceAt to replace the old schema with the new schema, before we continue processing.

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

This results in the following documents:

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