---
name: "forEachReversed"
type: command
status: ok
groups: ["Program Flow", "Arrays"]
arma3_since: "2.14"
locality: {"argument": null, "effect": null, "server_exec": false}
source: https://community.bistudio.com/wiki/forEachReversed
revid: 353029
updated: 2024-03-12T11:42:13Z
---

# forEachReversed

*Command · Program Flow, Arrays · Arma 3 v2.14*

## Description

Executes the given command(s) on every item of an [Array](../topics/Array.md) in reversed order, compared to [forEach](../commands/forEach.md).

## Syntax

```sqf
code forEachReversed array
```

**Parameters:**

- `code`: [Code](../topics/Code.md) to execute on each array item - available variables:
  - `_x`: iterated item
  - `_forEachIndex`: item's index
- `array`: [Array](../topics/Array.md) - the array to iterate over

**Returns:** [Anything](../topics/Anything.md) - will return the value of last executed statement

## Examples

### Example 1

This command lets you easily iterate through an array from last to first element without modifying the array itself with [reverse](../commands/reverse.md) or using [for](../commands/for.md) loop with negative [step](../commands/step.md).

```sqf
// Will show numbers from 5 to 1 in the chat
{ systemChat str _x } forEachReversed [1, 2, 3, 4, 5];
```

### Example 2

Can be useful to walk through array while also deleting items from it, which you couldn't do with [forEach](../commands/forEach.md) as easily:

```sqf
private _array = [1, 1, 2, 2, 1, 1, 2, 2];
{
    if (_x == 2) then { _array deleteAt _forEachIndex };
} forEachReversed _array;
// _array will be [1,1,1,1] here
```

Will not work with [forEach](../commands/forEach.md) as you might expect it:

```sqf
private _array = [1, 1, 2, 2, 1, 1, 2, 2];
{
    if (_x == 2) then { _array deleteAt _forEachIndex };
} forEach _array;
// _array will be [1, 1, 2, 1, 1, 2] here
```

### Example 3

Deleting items from array with more complex condition than just comparison.

```sqf
{
    if (!alive _x) then { _vehicles deleteAt _forEachIndex };
} forEachReversed _vehicles;
```

## See also

[Control Structures](../topics/Control_Structures.md) · [forEach](../commands/forEach.md) · [for](../commands/for.md) · [apply](../commands/apply.md) · [while](../commands/while.md) · [select](../commands/select.md) · [findIf](../commands/findIf.md) · [count](../commands/count.md) · [deleteAt](../commands/deleteAt.md) · [reverse](../commands/reverse.md)

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