Fusselwurm
(to anyone else wondering, I took a minute to get it) this is Array.map() in JavaScript
Results appear below as you type; use the arrow keys to move through them.
Applies the given code to each element of the given data structure and collects the results in an array.
array apply code
Parameters:
array: Array of Anythingcode: Code - code to be executed on each element of the array. The current element value is stored in the magic variable _x.Returns: Array of Anything - resulting array
since arma3 2.04
hashmap apply code
Parameters:
hashmap: HashMapcode: 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: Array of Anything - resulting array
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]
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]
private _arr1 = [];
_arr1 resize 20;
_arr2 = _arr1 apply { 0 }; // [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
[0,1,2,3,4] apply { str _x }; // ["0","1","2","3","4"]
private _hashmap = createHashMapFromArray [["Key 1", "Value 1"], ["Key 2", "Value 2"]];
private _array = _hashmap apply { _y + " Test" }; // ["Value 2 Test","Value 1 Test"]
set · resize · pushBack · pushBackUnique · select · reverse · count · find · in · forEach · deleteAt · deleteRange · append · sort · arrayIntersect
(to anyone else wondering, I took a minute to get it) this is Array.map() in JavaScript
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).