---
name: "apply"
type: command
status: ok
groups: ["Arrays", "HashMap"]
arma3_since: "1.56"
locality: {"argument": null, "effect": null, "server_exec": false}
source: https://community.bistudio.com/wiki/apply
revid: 377840
updated: 2026-01-03T04:33:29Z
---

# apply

*Command · Arrays, HashMap · Arma 3 v1.56*

## Description

Applies the given code to each element of the given data structure and collects the results in an array.

## Syntax

### Syntax 1

```sqf
array apply code
```

**Parameters:**

- `array`: [Array](../topics/Array.md) of [Anything](../topics/Anything.md)
- `code`: [Code](../topics/Code.md) - code to be executed on each element of the array. The current element value is stored in the magic variable [_x](../topics/Magic_Variables.md#x).

**Returns:** [Array](../topics/Array.md) of [Anything](../topics/Anything.md) - resulting array

### Syntax 2

*since arma3 2.04*

```sqf
hashmap apply code
```

**Parameters:**

- `hashmap`: [HashMap](../topics/HashMap.md)
- `code`: [Code](../topics/Code.md) - code to be executed on each key-value pair of the hashmap. The current key is stored in the magic variable [_x](../topics/Magic_Variables.md#x), the corresponding value is stored in [_y](../topics/Magic_Variables.md#y).

**Returns:** [Array](../topics/Array.md) of [Anything](../topics/Anything.md) - resulting array

## Examples

### Example 1

```sqf
private _arr = [1,2,3,4,5,6,7,8,9,0] apply { [1,0] select (_x % 2 == 0) }; // [1,0,1,0,1,0,1,0,1,0]
```

### Example 2

```sqf
private _arr = [1,2,3,4,5,6,7,8,9,0] apply { _x ^ _x }; // [1,4,27,256,3125,46656,823543,16777216,387420480,1]
```

### Example 3

```sqf
private _arr1 = [];
_arr1 resize 20;
_arr2 = _arr1 apply { 0 }; // [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
```

### Example 4

```sqf
[0,1,2,3,4] apply { str _x }; // ["0","1","2","3","4"]
```

### Example 5

```sqf
private _hashmap = createHashMapFromArray [["Key 1", "Value 1"], ["Key 2", "Value 2"]];
private _array = _hashmap apply { _y + " Test" }; // ["Value 2 Test","Value 1 Test"]
```

## See also

[set](../commands/set.md) · [resize](../commands/resize.md) · [pushBack](../commands/pushBack.md) · [pushBackUnique](../commands/pushBackUnique.md) · [select](../commands/select.md) · [reverse](../commands/reverse.md) · [count](../commands/count.md) · [find](../commands/find.md) · [in](../commands/in.md) · [forEach](../commands/forEach.md) · [deleteAt](../commands/deleteAt.md) · [deleteRange](../commands/deleteRange.md) · [append](../commands/append.md) · [sort](../commands/sort.md) · [arrayIntersect](../commands/arrayIntersect.md)

## Community notes

**Fusselwurm** — 2016-02-18

(to anyone else wondering, I took a minute to get it) this is [Array.map()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) in JavaScript

---

**Lou Montana** — 2018-02-11

if performance really is an issue, [apply](../commands/apply.md) seems to be (very) slightly faster than [forEach](../commands/forEach.md) (by more or less one percent, 0.7-1.5% in my tests to be precise).

<sub>Source: [BI Community Wiki — apply](https://community.bistudio.com/wiki/apply) (revision 377840).</sub>
