{"name":"apply","operator":null,"title":"apply","type":"command","url":"https://community.bistudio.com/wiki/apply","revid":377840,"updated":"2026-01-03T04:33:29Z","groups":["Arrays","HashMap"],"status":"ok","replaced_by":[],"games":[{"game":"arma3","name":"Arma 3","version":"1.56"}],"since":{"game":"arma3","name":"Arma 3","version":"1.56"},"arma3_since":"1.56","branch":null,"locality":{"argument":null,"effect":null,"server_exec":false},"exec":null,"description":"Applies the given code to each element of the given data structure and collects the results in an array.","syntaxes":[{"syntax":"array apply code","since":null,"effect":null,"exec":null,"params":[{"name":"array","type":"Array of Anything","optional":false,"default":null,"description":null,"types":["Array","Anything"],"since":null,"text":"array: Array of Anything"},{"name":"code","type":"Code","optional":false,"default":null,"description":"code to be executed on each element of the array. The current element value is stored in the magic variable _x.","types":["Code"],"since":null,"text":"code: Code - code to be executed on each element of the array. The current element value is stored in the magic variable _x."}],"returns":{"type":"Array of Anything","types":["Array","Anything"],"description":"resulting array","text":"Array of Anything - resulting array"},"form":"binary","operator":null,"left":{"text":"array","types":["Array","Anything"]},"right":{"text":"code","types":["Code"]}},{"syntax":"hashmap apply code","since":"arma3 2.04","effect":null,"exec":null,"params":[{"name":"hashmap","type":"HashMap","optional":false,"default":null,"description":null,"types":["HashMap"],"since":null,"text":"hashmap: HashMap"},{"name":"code","type":"Code","optional":false,"default":null,"description":"code to be executed on each key-value pair of the hashmap. The current key is stored in the magic variable _x, the corresponding value is stored in _y.","types":["Code"],"since":null,"text":"code: Code - code to be executed on each key-value pair of the hashmap. The current key is stored in the magic variable _x, the corresponding value is stored in _y."}],"returns":{"type":"Array of Anything","types":["Array","Anything"],"description":"resulting array","text":"Array of Anything - resulting array"},"form":"binary","operator":null,"left":{"text":"hashmap","types":["HashMap"]},"right":{"text":"code","types":["Code"]}}],"examples":[{"text":"```sqf\nprivate _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]\n```","code":["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]"]},{"text":"```sqf\nprivate _arr = [1,2,3,4,5,6,7,8,9,0] apply { _x ^ _x }; // [1,4,27,256,3125,46656,823543,16777216,387420480,1]\n```","code":["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]"]},{"text":"```sqf\nprivate _arr1 = [];\n_arr1 resize 20;\n_arr2 = _arr1 apply { 0 }; // [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]\n```","code":["private _arr1 = [];\n_arr1 resize 20;\n_arr2 = _arr1 apply { 0 }; // [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]"]},{"text":"```sqf\n[0,1,2,3,4] apply { str _x }; // [\"0\",\"1\",\"2\",\"3\",\"4\"]\n```","code":["[0,1,2,3,4] apply { str _x }; // [\"0\",\"1\",\"2\",\"3\",\"4\"]"]},{"text":"```sqf\nprivate _hashmap = createHashMapFromArray [[\"Key 1\", \"Value 1\"], [\"Key 2\", \"Value 2\"]];\nprivate _array = _hashmap apply { _y + \" Test\" }; // [\"Value 2 Test\",\"Value 1 Test\"]\n```","code":["private _hashmap = createHashMapFromArray [[\"Key 1\", \"Value 1\"], [\"Key 2\", \"Value 2\"]];\nprivate _array = _hashmap apply { _y + \" Test\" }; // [\"Value 2 Test\",\"Value 1 Test\"]"]}],"multiplayer":null,"problems":null,"aliases":[],"see_also":["set","resize","pushBack","pushBackUnique","select","reverse","count","find","in","forEach","deleteAt","deleteRange","append","sort","arrayIntersect"],"notes":[{"user":"Fusselwurm","date":"2016-02-18T11:03:00Z","text":"(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"},{"user":"Lou Montana","date":"2018-02-11T23:02:00Z","text":"if performance really is an issue, apply seems to be (very) slightly faster than forEach (by more or less one percent, 0.7-1.5% in my tests to be precise)."}],"categories":["Arma 3: New Scripting Commands","Arma 3: Scripting Commands","Command Group: Arrays","Command Group: HashMap","Introduced with Arma 3 version 1.56","Scripting Commands"]}