apply: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (template:command argument fix)
m (template:command argument fix)
Line 13: Line 13:
| array '''apply''' code|SYNTAX=
| array '''apply''' code|SYNTAX=


|p1= array: [[Array]] - array of [[Anything]] |= Parameter 1
|p1= array: [[Array]] - array of [[Anything]] |PARAMETER1=


|p2= code: [[Code]] - code to be executed on each element of the array. Current element value is stored in variable [[_x]]|= Parameter 2
|p2= code: [[Code]] - code to be executed on each element of the array. Current element value is stored in variable [[_x]]|PARAMETER2=


| [[Array]] - resulting array|RETURNVALUE=
| [[Array]] - resulting array|RETURNVALUE=
____________________________________________________________________________________________
____________________________________________________________________________________________
   
   
|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> |= Example 1
|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> |EXAMPLE1=
|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> |= Example 2
|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> |EXAMPLE2=
|x3= <code>_arr1 = [];
|x3= <code>_arr1 = [];
_arr1 [[resize]] 20;
_arr1 [[resize]] 20;

Revision as of 15:28, 7 April 2019

Hover & click on the images for description

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:
Uncategorised

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

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]

Additional Information

See also:
setresizepushBackpushBackUniqueselectreversecountfindinforEachdeleteAtdeleteRangeappendsortarrayIntersectBIS_fnc_arrayPushStackBIS_fnc_arrayPush

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

Notes

Bottom Section

Posted on February 18, 2016 - 11:03 (UTC)
Fusselwurm
(to anyone else wondering, I took a minute to get it): This is Array.map() is 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).