# `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](https://coralogix.com/docs/dataprime/language-reference/functions-reference/index.md), **function** and **method** notation. These interchangeable forms allow flexibility in how you structure expressions.

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

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

## Arguments

| Name     | Type   | Required | Description                                      |
| -------- | ------ | -------- | ------------------------------------------------ |
| array    | array  | **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:

```json
{
    "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

```dataprime
create updated_values from arrayReplaceAt(values, 0, 'NewVal1')
```

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

### Example output

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

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