---
description: Insert method of array helpers
---

# $.array.insert

Insert is an array method from array helper that we use to inject or add the data into a new array. We create this method as a function closure, so we can use spread parameter and can be insert multiple data at the same time.

#### Insert data at the first element of array

```
$.array.insert(fromArray, atObject, ...allValues*)
//atObject={position: 'first', index: 0} last, before or after

//or
$.array.insertFirst(fromArray, ...allValues*)

// Example:
const fromArray = ['item 1', 'item 2']
$.array.insertFirst(fromArray, 'item 3')
//=> ['item 3', 'item 1', 'item 2']

//or
$.array.insert(fromArray, {position: 'first'}, 'new item')
```

#### Insert data at the last element of array

```
$.array.insertLast(fromArray, ...allValues*)

// Example:
$.array.insertLast(fromArray, 'item 3')
//=> ['item 1', 'item 2', 'item 3']

//or
$.array.insert(fromArray, {position: 'last'}, 'new item')
```

#### Insert data before any element by index

```
$.array.insertBefore(fromArray, beforeIndex, ...allValues*)

// Example:
$.array.insertBefore(fromArray, 2, 'new text')

//or
$.array.insert(fromArray, {position: 'before', index: 2}, 'new item')
```

#### Insert data after any element by index

```
$.array.insertAfter(fromArray, afterIndex, ...allValues*)

// Example:
$.array.insertAfter(fromArray, 2, 'new text')

//or
$.array.insert(fromArray, {position: 'after', index: 2}, 'new item')
```

#### Insert multiple data to the array

```
$.array.insertFirst(fromArray, 'text', 1, {}, [])

//or
$.array.insertLast(fromArray, 'string', 1.5, {'key': 'value'}, [])
```
