{"name":"forEach","title":"forEach","type":"command","url":"https://community.bistudio.com/wiki/forEach","revid":378167,"updated":"2026-03-02T16:49:17Z","groups":["Program Flow","Arrays","HashMap"],"status":"ok","games":{"ofp":"1.00","ofpe":"1.00","arma1":"1.00","arma2":"1.00","arma2oa":"1.50","tkoh":"1.00","arma3":"0.50"},"since":"ofp 1.00","arma3_since":"0.50","description":"Executes the given command(s) on every item of an Array or a HashMap.\n\n> [!NOTE]\n> **Arma 2:** Since Arma 2, the variable _x is always private to the forEach block so it is safe to nest them (see Example 8).","syntaxes":[{"syntax":"code forEach array","params":[{"name":"code","type":"String only in Operation Flashpoint until v1.85 or Arma: Cold War Assault v1.85 Code","description":"available variables:\n\n- `_x`: iterated item\n- Arma 2: Operation Arrowhead v1.55 `_forEachIndex`: item's index\n\n|  |  |  |  |  |  |  |  |\n|---|---|---|---|---|---|---|---|\n| Game | Operation Flashpoint v1.00 | Operation Flashpoint v1.85 | Armed Assault v1.00 | Arma 2 v1.00 | Arma 2: Operation Arrowhead v1.50 | Arma 3 v1.00 | Take On Helicopters v1.00 |\n| String support | ✓ |  |  | ✗ |  |  |  |\n| Code support | ✗ | ✓ |  |  |  |  |  |","type_expr":"String | Code"},{"name":"array","type":"Array","description":"the array to iterate over","type_expr":"Array"}],"returns":{"type":"Anything","type_expr":"Anything","description":"will return the value of last executed statement"},"form":"binary","left":{"text":"code"},"right":{"text":"array"}},{"syntax":"code forEach hashMap","since":"arma3 2.02","params":[{"name":"code","type":"Code","description":"code applied to each key-value pair - available variables:\n\n- `_x`: key\n- `_y`: value\n- `_forEachIndex`: iteration number","type_expr":"Code"},{"name":"hashMap","type":"HashMap","description":"the HashMap to iterate over","type_expr":"HashMap","text":"hashMap : HashMap - the HashMap to iterate over"}],"returns":{"type":"Anything","type_expr":"Anything","description":"will return the value of last executed statement"},"form":"binary","left":{"text":"code"},"right":{"text":"hashMap"}}],"examples":[{"text":"```sqf\n// SQF\n{ _x setDamage 1 } forEach units player;\n```\n\n```sqs\n; SQS\n\"_x setDammage 1\" forEach units player\n```","code":["// SQF\n{ _x setDamage 1 } forEach units player;","; SQS\n\"_x setDammage 1\" forEach units player"]},{"text":"This command can also easily be used to execute a single command multiple times without respect to the array items - see also for\n\n```sqf\n{ player addMagazine \"30Rnd_556x45_Stanag\" } forEach [1, 2, 3, 4];\n// equivalent to\nfor \"_i\" from 1 to 4 do { player addMagazine \"30Rnd_556x45_Stanag\" };\n```","code":["{ player addMagazine \"30Rnd_556x45_Stanag\" } forEach [1, 2, 3, 4];\n// equivalent to\nfor \"_i\" from 1 to 4 do { player addMagazine \"30Rnd_556x45_Stanag\" };"]},{"text":"You can also use multiple commands in the same block:\n\n```sqf\n{\n    _x setCaptive true;\n    removeAllWeapons _x;\n    doStop _x;\n} forEach units group this;\n```","code":["{\n\t_x setCaptive true;\n\tremoveAllWeapons _x;\n\tdoStop _x;\n} forEach units group this;"]},{"text":"To get the index of a forEach loop, use _forEachIndex:\n\n```sqf\n{ systemChat str _forEachIndex; } forEach [\"a\", \"b\", \"c\"]; // will return: \"0\", \"1\", \"2\" in systemChat messages\n```","code":["{ systemChat str _forEachIndex; } forEach [\"a\", \"b\", \"c\"]; // will return: \"0\", \"1\", \"2\" in systemChat messages"]},{"text":"Iterating a HashMap's _forEachIndex:\n\n```sqf\n// shows \"0, k1, v1\", \"1, k2, v2\" in systemChat messages\n{\n    systemChat format [\"%1, %2, %3\", _forEachIndex, _x, _y];\n} forEach createHashMapFromArray [\n    [\"k1\", \"v1\"],\n    [\"k2\", \"v2\"]\n];\n```","code":["// shows \"0, k1, v1\", \"1, k2, v2\" in systemChat messages\n{\n\tsystemChat format [\"%1, %2, %3\", _forEachIndex, _x, _y];\n} forEach createHashMapFromArray [\n\t[\"k1\", \"v1\"],\n\t[\"k2\", \"v2\"]\n];"]},{"text":"findIf equivalent for HashMap:\n\n```sqf\nprivate _resultKey = {\n    if (_y isEqualTo \"wantedValue\") exitWith { _x };\n    \"\"\n} forEach _hashmap;\n```","code":["private _resultKey = {\n\tif (_y isEqualTo \"wantedValue\") exitWith { _x };\n\t\"\"\n} forEach _hashmap;"]},{"text":"Array is edited by reference:\n\n```sqf\n_arr1 = [1,2,3];\n_arr2 = [6,7,8];\n_arr3 = [0];\n{ _x set [1, \"changed\"] } forEach [_arr1, _arr2, _arr3];\n// _arr1 = [1, \"changed\", 3]\n// _arr2 = [6, \"changed\", 8]\n// _arr3 = [0, \"changed\"]\n```","code":["_arr1 = [1,2,3];\n_arr2 = [6,7,8];\n_arr3 = [0];\n{ _x set [1, \"changed\"] } forEach [_arr1, _arr2, _arr3];\n// _arr1 = [1, \"changed\", 3]\n// _arr2 = [6, \"changed\", 8]\n// _arr3 = [0, \"changed\"]"]},{"text":"```sqf\n{\n    private _verticalValue = _x; // needed, otherwise _horizontalValues' _x made this one inaccessible\n    {\n        [_x, _verticalValue] call TAG_fnc_doSomething;\n    } forEach _horizontalValues;\n} forEach _verticalValues;\n```","code":["{\n\tprivate _verticalValue = _x; // needed, otherwise _horizontalValues' _x made this one inaccessible\n\t{\n\t\t[_x, _verticalValue] call TAG_fnc_doSomething;\n\t} forEach _horizontalValues;\n} forEach _verticalValues;"]}],"see_also":["Control Structures","for","apply","while","select","findIf","count","forEachReversed"],"notes":[{"user":"Dedmen","date":"2017-11-28T13:46:00Z","text":"Be careful when deleting (deleteAt) elements from an Array while you iterate over it.\n_forEachIndex will not move to reflect your change.\n\nThe forEach code is doing the same as\n\n```sqf\nprivate _forEachIndex = 0;\nwhile { _forEachIndex < count _array } do\n{\n    (_array select _forEachIndex) call code;\n    _forEachIndex = _forEachIndex + 1;\n};\n```\n\nSo if you delete your current element from the array the other elements will shift forward. Meaning you skip one element.\nExample:\n\n```sqf\n_array = [1,2,3,4,5,6];\n{ _array deleteAt _forEachIndex } forEach _array;\n```\n\nAfter the first iteration your Array will be [2,3,4,5,6] and the _forEachIndex will be 1.\nSo on next iteration you get the element at index 1 which will be 3. So you've just skipped the 2.\nSo in the end you will only iterate over 1, 3 and 6."},{"user":"Sa-Matra","date":"2023-04-02T09:04:28Z","text":"Use new forEachReversed command for deleting array items with deleteAt. Check its examples for details."}]}