{"name":"apply","title":"apply","type":"command","url":"https://community.bistudio.com/wiki/apply","revid":377840,"updated":"2026-01-03T04:33:29Z","groups":["Arrays","HashMap"],"status":"ok","games":{"arma3":"1.56"},"since":"arma3 1.56","arma3_since":"1.56","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","params":[{"name":"array","type":"Array of Anything","type_expr":"Array<Anything>"},{"name":"code","type":"Code","description":"code to be executed on each element of the array. The current element value is stored in the magic variable _x.","type_expr":"Code"}],"returns":{"type":"Array of Anything","type_expr":"Array<Anything>","description":"resulting array"},"form":"binary","left":{"text":"array"},"right":{"text":"code"}},{"syntax":"hashmap apply code","since":"arma3 2.04","params":[{"name":"hashmap","type":"HashMap","type_expr":"HashMap"},{"name":"code","type":"Code","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.","type_expr":"Code"}],"returns":{"type":"Array of Anything","type_expr":"Array<Anything>","description":"resulting array"},"form":"binary","left":{"text":"hashmap"},"right":{"text":"code"}}],"examples":[{"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]"]},{"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]"]},{"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]"]},{"code":["[0,1,2,3,4] apply { str _x }; // [\"0\",\"1\",\"2\",\"3\",\"4\"]"]},{"code":["private _hashmap = createHashMapFromArray [[\"Key 1\", \"Value 1\"], [\"Key 2\", \"Value 2\"]];\nprivate _array = _hashmap apply { _y + \" Test\" }; // [\"Value 2 Test\",\"Value 1 Test\"]"]}],"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)."}]}