{"name":"forEachReversed","operator":null,"title":"forEachReversed","type":"command","url":"https://community.bistudio.com/wiki/forEachReversed","revid":353029,"updated":"2024-03-12T11:42:13Z","groups":["Program Flow","Arrays"],"status":"ok","replaced_by":[],"games":[{"game":"arma3","name":"Arma 3","version":"2.14"}],"since":{"game":"arma3","name":"Arma 3","version":"2.14"},"arma3_since":"2.14","branch":null,"locality":{"argument":null,"effect":null,"server_exec":false},"exec":null,"description":"Executes the given command(s) on every item of an Array in reversed order, compared to forEach.","syntaxes":[{"syntax":"code forEachReversed array","since":null,"effect":null,"exec":null,"params":[{"name":"code","type":"Code to execute on each array item","optional":false,"default":null,"description":"available variables:\n- `_x`: iterated item\n- `_forEachIndex`: item's index","types":["Code"],"since":null,"text":"code: Code to execute on each array item - available variables:\n- `_x`: iterated item\n- `_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":["Code"]},"right":{"text":"array","types":["Array"]}}],"examples":[{"text":"This command lets you easily iterate through an array from last to first element without modifying the array itself with reverse or using for loop with negative step.\n\n```sqf\n// Will show numbers from 5 to 1 in the chat\n{ systemChat str _x } forEachReversed [1, 2, 3, 4, 5];\n```","code":["// Will show numbers from 5 to 1 in the chat\n{ systemChat str _x } forEachReversed [1, 2, 3, 4, 5];"]},{"text":"Can be useful to walk through array while also deleting items from it, which you couldn't do with forEach as easily:\n\n```sqf\nprivate _array = [1, 1, 2, 2, 1, 1, 2, 2];\n{\n    if (_x == 2) then { _array deleteAt _forEachIndex };\n} forEachReversed _array;\n// _array will be [1,1,1,1] here\n```\n\nWill not work with forEach as you might expect it:\n\n```sqf\nprivate _array = [1, 1, 2, 2, 1, 1, 2, 2];\n{\n    if (_x == 2) then { _array deleteAt _forEachIndex };\n} forEach _array;\n// _array will be [1, 1, 2, 1, 1, 2] here\n```","code":["private _array = [1, 1, 2, 2, 1, 1, 2, 2];\n{\n\tif (_x == 2) then { _array deleteAt _forEachIndex };\n} forEachReversed _array;\n// _array will be [1,1,1,1] here","private _array = [1, 1, 2, 2, 1, 1, 2, 2];\n{\n\tif (_x == 2) then { _array deleteAt _forEachIndex };\n} forEach _array;\n// _array will be [1, 1, 2, 1, 1, 2] here"]},{"text":"Deleting items from array with more complex condition than just comparison.\n\n```sqf\n{\n    if (!alive _x) then { _vehicles deleteAt _forEachIndex };\n} forEachReversed _vehicles;\n```","code":["{\n\tif (!alive _x) then { _vehicles deleteAt _forEachIndex };\n} forEachReversed _vehicles;"]}],"multiplayer":null,"problems":null,"aliases":[],"see_also":["Control Structures","forEach","for","apply","while","select","findIf","count","deleteAt","reverse"],"notes":[],"categories":["Arma 3: New Scripting Commands","Arma 3: Scripting Commands","Command Group: Arrays","Command Group: Program Flow","Introduced with Arma 3 version 2.14","Scripting Commands"]}