{"name":"forEach","operator":null,"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","replaced_by":[],"games":[{"game":"ofp","name":"Operation Flashpoint","version":"1.00"},{"game":"ofpe","name":"Operation Flashpoint: Elite","version":"1.00"},{"game":"arma1","name":"Armed Assault","version":"1.00"},{"game":"arma2","name":"Arma 2","version":"1.00"},{"game":"arma2oa","name":"Arma 2: Operation Arrowhead","version":"1.50"},{"game":"tkoh","name":"Take On Helicopters","version":"1.00"},{"game":"arma3","name":"Arma 3","version":"0.50"}],"since":{"game":"ofp","name":"Operation Flashpoint","version":"1.00"},"arma3_since":"0.50","branch":null,"locality":{"argument":null,"effect":null,"server_exec":false},"exec":null,"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","since":null,"effect":null,"exec":null,"params":[{"name":"|  |  |  |  |  |  |  |  |","type":null,"optional":false,"default":null,"description":"|  |  |  |  |  |  |  |  |\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 | ✗ | ✓ |  |  |  |  |  |\n\ncode: String only in Operation Flashpoint until v1.85 or Arma: Cold War Assault v1.85 Code - available variables:\n- `_x`: iterated item\n- Arma 2: Operation Arrowhead v1.55 `_forEachIndex`: item's index","types":[],"since":null,"text":"|  |  |  |  |  |  |  |  |\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 | ✗ | ✓ |  |  |  |  |  |\n\ncode: String only in Operation Flashpoint until v1.85 or Arma: Cold War Assault v1.85 Code - available variables:\n- `_x`: iterated item\n- Arma 2: Operation Arrowhead v1.55 `_forEachIndex`: item's index"},{"name":"array","type":"Array","optional":false,"default":null,"description":"the array to iterate over","types":["Array"],"since":null,"text":"array: Array - the array to iterate over"}],"returns":{"type":"Anything","types":["Anything"],"description":"will return the value of last executed statement","text":"Anything - will return the value of last executed statement"},"form":"binary","operator":null,"left":{"text":"code","types":[]},"right":{"text":"array","types":["Array"]}},{"syntax":"code forEach hashMap","since":"arma3 2.02","effect":null,"exec":null,"params":[{"name":"code","type":"Code","optional":false,"default":null,"description":"code applied to each key-value pair - available variables:\n- `_x`: key\n- `_y`: value\n- `_forEachIndex`: iteration number","types":["Code"],"since":null,"text":"code: Code - code applied to each key-value pair - available variables:\n- `_x`: key\n- `_y`: value\n- `_forEachIndex`: iteration number"},{"name":"hashMap","type":"HashMap","optional":false,"default":null,"description":"the HashMap to iterate over","types":["HashMap"],"since":null,"text":"hashMap : HashMap - the HashMap to iterate over"}],"returns":{"type":"Anything","types":["Anything"],"description":"will return the value of last executed statement","text":"Anything - will return the value of last executed statement"},"form":"binary","operator":null,"left":{"text":"code","types":["Code"]},"right":{"text":"hashMap","types":["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;"]}],"multiplayer":null,"problems":null,"aliases":[],"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\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.\n\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.\n\nSo on next iteration you get the element at index 1 which will be 3. So you've just skipped the 2.\n\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."}],"categories":["ArmA: Armed Assault: Scripting Commands","Arma 2: Operation Arrowhead: Scripting Commands","Arma 2: Scripting Commands","Arma 3: Scripting Commands","Command Group: Arrays","Command Group: HashMap","Command Group: Program Flow","Introduced with Operation Flashpoint version 1.00","Operation Flashpoint: Elite: Scripting Commands","Operation Flashpoint: New Scripting Commands","Operation Flashpoint: Scripting Commands","Scripting Commands","Take On Helicopters: Scripting Commands"]}