Skip to content

arrayReplaceAll

Description

Returns a new array where all instances of a specified value are replaced with 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, 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
arrayarraytrueThe array to modify
valueTtrueThe value to replace
newValueTtrueThe replacement value, must match the array type

Example

Use case: Replace outdated schema values in arrays

Suppose you have a list of values where some still use an outdated schema. Consider the following inputs:

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

By replacing all occurrences of "OldVal1" with "NewVal1", you ensure data consistency before further processing.

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

Output

The result will replace all outdated values:

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