ArrayMap
Iterates over every entry of the array and calls the closure function to work on the element of the array.
The returned value will be set at the same index in a new array and the new array will be returned
Method Signature
ArrayMap(array=[array], callback=[function:Function], parallel=[boolean], maxThreads=[any], virtual=[boolean])
Arguments
array
array
true
The array to reduce
callback
function:Function
true
The function to invoke for each item. The function will be passed 3 arguments: the current item, and the current index, and the original array. You can alternatively pass a Java Function which will only receive the 1st arg. The function should return the value that will be set at the same index in the new array.
parallel
boolean
false
If true, the function will be invoked in parallel using multiple threads. Defaults to false.
false
maxThreads
any
false
The maximum number of threads to use when parallel is true. If not provided the common thread pool will be used. If a boolean value is passed, it will be assigned as the virtual argument.
virtual
boolean
false
If true, the function will be invoked using virtual thread. Defaults to false. Ingored if parallel is false.
false
Examples
Script Syntax
complexData = [
{
A : 4
},
{
A : 18
},
{
A : 51
}
];
newArray = arrayMap( complexData, ( Any item ) => {
return item.A;
} );
writeDump( newArray );
Result: [4, 18, 51]
Additional Examples
aNames = [
"Marcus",
"Sarah",
"Josefine"
];
dump( aNames );
newNames1 = arrayMap( aNames, ( Any item, Any index, Any arr ) => {
return {
"name" : item
};
} );
dump( newNames1 );
// member function
newNames2 = aNames.map( ( Any item, Any index, Any arr ) => {
return {
"name" : item
};
} );
dump( newNames2 );
Related
Last updated
Was this helpful?