Skip to content

arrayReplaceAll - Replaces all instances of a value with a new value

arrayReplaceAll will replace all instances of a value in a given array with a new value.

Syntax

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

arrayReplaceAll(array: array<T>, value: T, newValue: T): array<T>
array: array<T>.arrayReplaceAll(value: T, newValue: T): array<T>

Arguments

NameTypeRequiredDescription
arrayarray of type TtrueT must be either string, bool, number, interval, timestamp, regexp or enum
valueTtrueT must be either string, bool, number, interval, timestamp, regexp or enum
newValueTtrueT must be either string, bool, number, interval, timestamp, regexp or enum

Example - Cleaning up old values in lists

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 arrayReplaceAll to replace the old schema with the new schema, before we continue processing.

create updated_values from arrayReplaceAll(values, 'OldVal1', 'NewVal1')
create updated_values from values.arrayReplaceAll('OldVal1', 'NewVal1')

This results in the following documents:

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