apply: Difference between revisions
Jump to navigation
Jump to search
Lou Montana (talk | contribs) m (Text replacement - "\|x([0-9])= *<code>([^<]*)<\/code>" to "|x$1= <sqf>$2</sqf>") |
Lou Montana (talk | contribs) m (Some wiki formatting) |
||
Line 13: | Line 13: | ||
|p1= array: [[Array]] - Array of [[Anything]] | |p1= array: [[Array]] - Array of [[Anything]] | ||
|p2= code: [[Code]] - | |p2= code: [[Code]] - code to be executed on each element of the array. The current element value is stored in the magic variable [[Magic Variables#x|_x]]. | ||
|r1= [[Array]] - | |r1= [[Array]] - resulting array | ||
|s2= hashmap [[apply]] code | |s2= hashmap [[apply]] code | ||
|s2since= arma3 2.04 | |s2since= arma3 2.04 | ||
Line 24: | Line 25: | ||
|p22= code: [[Code]] - Code to be executed on each key-value pair of the hashmap. The current key is stored in the magic variable [[Magic Variables#x|_x]], the corresponding value is stored in [[Magic Variables#y|_y]]. | |p22= code: [[Code]] - Code to be executed on each key-value pair of the hashmap. The current key is stored in the magic variable [[Magic Variables#x|_x]], the corresponding value is stored in [[Magic Variables#y|_y]]. | ||
|r2= [[Array]] - | |r2= [[Array]] - resulting array | ||
|x1= <sqf>_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]</sqf> | |x1= <sqf>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]</sqf> | ||
|x2= <sqf>_arr = [1,2,3,4,5,6,7,8,9,0] apply {_x ^ _x}; // [1,4,27,256,3125,46656,823543,16777216,387420480,1]</sqf> | |x2= <sqf>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]</sqf> | ||
|x3= <sqf>_arr1 = []; | |x3= <sqf> | ||
private _arr1 = []; | |||
_arr1 resize 20; | _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]</sqf> | _arr2 = _arr1 apply { 0 }; // [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] | ||
</sqf> | |||
|x4= <sqf>[0,1,2,3,4] apply {str _x}; // ["0","1","2","3","4"]</sqf> | |x4= <sqf>[0,1,2,3,4] apply { str _x }; // ["0","1","2","3","4"]</sqf> | ||
|x5= <sqf>_hashmap = createHashMapFromArray [["Key 1", "Value 1"], ["Key 2", "Value 2"]]; | |x5= <sqf> | ||
_array = _hashmap apply {_y + " Test"}; // ["Value 2 Test","Value 1 Test"]</sqf> | private _hashmap = createHashMapFromArray [["Key 1", "Value 1"], ["Key 2", "Value 2"]]; | ||
private _array = _hashmap apply { _y + " Test" }; // ["Value 2 Test","Value 1 Test"] | |||
</sqf> | |||
|seealso= [[set]] [[resize]] [[pushBack]] [[pushBackUnique]] [[select]] [[reverse]] [[count]] [[find]] [[in]] [[forEach]] [[deleteAt]] [[deleteRange]] [[append]] [[sort]] [[arrayIntersect]] | |seealso= [[set]] [[resize]] [[pushBack]] [[pushBackUnique]] [[select]] [[reverse]] [[count]] [[find]] [[in]] [[forEach]] [[deleteAt]] [[deleteRange]] [[append]] [[sort]] [[arrayIntersect]] | ||
}} | }} | ||
{{Note | |||
|user= Fusselwurm | |||
|timestamp= 20160218110300 | |||
|text= (to anyone else wondering, I took a minute to get it) this is [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map Array.map()] in JavaScript | |||
}} | |||
{{Note | |||
|user= Lou Montana | |||
|timestamp= 20180211230200 | |||
|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). | |||
}} | |||
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). | |||
Revision as of 15:34, 20 June 2022
Description
- Description:
- Applies the given code to each element of the given data structure and collects the results in an array.
- Groups:
- ArraysHashMap
Syntax
- Syntax:
- array apply code
- Parameters:
- array: Array - Array of Anything
- code: Code - code to be executed on each element of the array. The current element value is stored in the magic variable _x.
- Return Value:
- Array - resulting array
Alternative Syntax
- Syntax:
- hashmap apply code
- Parameters:
- hashmap: HashMap
- 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.
- Return Value:
- Array - resulting array
Examples
- Example 1:
- Example 2:
- Example 3:
- Example 4:
- Example 5:
Additional Information
- See also:
- set resize pushBack pushBackUnique select reverse count find in forEach deleteAt deleteRange append sort arrayIntersect
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 Feb 18, 2016 - 11:03 (UTC)
- (to anyone else wondering, I took a minute to get it) this is Array.map() in JavaScript