apply: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
m (formatting) |
||
Line 1: | Line 1: | ||
{{RV|type=command | {{RV|type=command | ||
|arma3 | |game1= arma3 | ||
|1.56 | |version1= 1.56 | ||
|gr1= Arrays | |gr1= Arrays | ||
| Applies given code to each element of the array and returns resulting array. The value of the current array element, to which the code will be applied, is stored in variable [[Magic Variables#x|_x]]. | |descr= Applies given code to each element of the array and returns resulting array. The value of the current array element, to which the code will be applied, is stored in variable [[Magic Variables#x|_x]]. | ||
|s1= array '''apply''' code | |s1= array '''apply''' code | ||
|p1= array: [[Array]] - | |p1= array: [[Array]] - Array of [[Anything]] | ||
|p2= code: [[Code]] - | |p2= code: [[Code]] - Code to be executed on each element of the array. Current element value is stored in variable [[Magic Variables#x|_x]] | ||
|r1= [[Array]] - | |r1= [[Array]] - Resulting array | ||
|s2= hashmap '''apply''' code | |s2= hashmap '''apply''' code | ||
Line 21: | Line 21: | ||
|p21= hashmap: [[HashMap]] | |p21= hashmap: [[HashMap]] | ||
|p22= code: [[Code]] - | |p22= code: [[Code]] - Code to be executed on each element of the hashmap. Current key is stored in variable [[Magic Variables#x|_x]], and the value is stored in variable [[Magic Variables#y|_y]] | ||
|r2= [[Array]] - | |r2= [[Array]] - Resulting array | ||
|x1= <code>_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> | |x1= <code>_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> | ||
|x2= <code>_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> | |x2= <code>_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> | ||
|x3= <code>_arr1 = []; | |x3= <code>_arr1 = []; | ||
_arr1 [[resize]] 20; | _arr1 [[resize]] 20; | ||
Line 32: | Line 34: | ||
|x4= <code>[0,1,2,3,4] [[apply]] {[[str]] _x}; {{cc|Returns ["0","1","2","3","4"]}}</code> | |x4= <code>[0,1,2,3,4] [[apply]] {[[str]] _x}; {{cc|Returns ["0","1","2","3","4"]}}</code> | ||
|x5= <code>_hashmap = [[createHashMapFromArray]] <nowiki>[["key", "value"]]</nowiki>; | |x5= <code>_hashmap = [[createHashMapFromArray]] <nowiki>[["key", "value"]]</nowiki>; | ||
_array = _hashmap [[apply]] {_y}; //["value"]</code> | _array = _hashmap [[apply]] {_y}; //["value"]</code> |
Revision as of 08:37, 19 May 2021
Description
- Description:
- Applies given code to each element of the array and returns resulting array. The value of the current array element, to which the code will be applied, is stored in variable _x.
- Groups:
- Arrays
Syntax
- Syntax:
- array apply code
- Parameters:
- array: Array - Array of Anything
- code: Code - Code to be executed on each element of the array. Current element value is stored in variable _x
- Return Value:
- Array - Resulting array
Alternative Syntax
- Syntax:
- hashmap apply code
- Parameters:
- hashmap: HashMap
- code: Code - Code to be executed on each element of the hashmap. Current key is stored in variable _x, and the value is stored in variable _y
- Return Value:
- Array - Resulting array
Examples
- Example 1:
_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]
- Example 2:
_arr = [1,2,3,4,5,6,7,8,9,0] apply {_x ^ _x}; //[1,4,27,256,3125,46656,823543,16777216,387420480,1]
- Example 3:
_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]
- Example 4:
[0,1,2,3,4] apply {str _x}; // Returns ["0","1","2","3","4"]
- Example 5:
_hashmap = createHashMapFromArray [["key", "value"]]; _array = _hashmap apply {_y}; //["value"]
Additional Information
- See also:
- setresizepushBackpushBackUniqueselectreversecountfindinforEachdeleteAtdeleteRangeappendsortarrayIntersect
Notes
-
Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note
- Posted on February 18, 2016 - 11:03 (UTC)
- Fusselwurm
- (to anyone else wondering, I took a minute to get it): This is Array.map() in JavaScript
- Posted on February 11, 2018 - 23:02 (UTC)
- Lou Montana
- 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).