Skip to content

arrayInsertAt - Add element at specified position in array

arrayInsertAt will add an element at a given position.

Syntax

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

arrayInsertAt(array: array<T>, position: number, value: T): array<T>
array: array<T>.arrayInsertAt(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 an element should be inserted
valueTtrueT must be either string, bool, number, interval, timestamp, regexp or enum

Example - Adding a job in the middle of a queue

Consider the following document:

{
    "values": ["Job 1", "Job 2", "Job 4"]
}

Inserting an item in a given point in an array is as simple as:

replace values with arrayInsertAt(values, 2, 'Job 3')
replace values with values.arrayInsertAt(2, 'Job 3')

This results in the following document:

{
    "values": ["Job 1", "Job 2", "Job 3", "Job 4"]
}

Try it yourself

Open up your explore screen and enter the following command:

create values from ['Job 1', 'Job 2', 'Job 4']
| create updated_values from values.arrayInsertAt(2, 'Job 3')