# ArrayEach

Used to iterate over an array and run the function closure for each item in the array.

This BIF is used to perform an operation on each item in the array, similar to Java's forEach method. It can also be used to perform operations in parallel if the `parallel` argument is set to true.

## Parallel Execution

If the `parallel` argument is set to true, and no `max_threads` are sent, the iterator will be executed in parallel using a ForkJoinPool with parallel streams. If `max_threads` is specified, it will create a new ForkJoinPool with the specified number of threads to run the iterator in parallel, and destroy it after the operation is complete. Please note that this may not be the most efficient way to iterate, as it will create a new ForkJoinPool for each invocation of the BIF. You may want to consider using a shared ForkJoinPool for better performance.

## Method Signature

```
ArrayEach(array=[array], callback=[function:Consumer], parallel=[boolean], maxThreads=[any], ordered=[boolean], virtual=[boolean])
```

### Arguments

| Argument     | Type                | Required | Description                                                                                                                                                                                                                                                                                     | Default |
| ------------ | ------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------- |
| `array`      | `array`             | `true`   | The array to reduce                                                                                                                                                                                                                                                                             |         |
| `callback`   | `function:Consumer` | `true`   | The function to invoke for each item. The function will be passed 3 arguments: the value, the index, the array. You can alternatively pass a Java Comparator which will only receive the 1st arg.                                                                                               |         |
| `parallel`   | `boolean`           | `false`  | Whether to run the filter in parallel. Defaults to false. If true, the filter will be run in parallel using a ForkJoinPool.                                                                                                                                                                     | `false` |
| `maxThreads` | `any`               | `false`  | <p>The maximum number of threads to use when running the filter in parallel. If not passed it will use the default number of threads for the ForkJoinPool.<br>If parallel is false, this argument is ignored. If a boolean is provided it will be assigned to the virtual argument instead.</p> |         |
| `ordered`    | `boolean`           | `false`  | (BoxLang only) whether parallel operations should execute and maintain order                                                                                                                                                                                                                    | `false` |
| `virtual`    | `boolean`           | `false`  | ( BoxLang only) If true, the function will be invoked using virtual threads. Defaults to false. Ignored if parallel is false.                                                                                                                                                                   | `false` |

## Examples

### Simple Example

[Run Example](https://try.boxlang.io/?code=eJzLSS0pSS0qVrBViFbg4lRKVNIBkklgMhlMpihxxVpzJRYVJVa6JiZnaCjkQHToKGgoOOZVKqTmpOam5pXogDmZeSmpFQqaCrZ2CtVcnOVFmSWp%2FqUlBaUlGgpKymBJZStlqA5layUFTWuuWhABAKk3JdU%3D)

```java
letters = [ 
	"a",
	"b",
	"c",
	"d"
];
arrayEach( letters, ( Any element, Any index ) => {
	writeOutput( "#index#:#element#;" );
} );

```

Result: 1:a;2:b;3:c;4:d;

### Member Function Example

[Run Example](https://try.boxlang.io/?code=eJxLVLBViFbg4lRKVNIBkklgMlmJK9aaK1EvNTE5Q0NBQ8Exr1IhNSc1NzWvRAfMycxLSa2AMBOLihIrFTQVbO0Uqrk4y4syS1L9S0sKSks0FJSUweqUrZShmpWtlRQ0rblqQQQAdHghQA%3D%3D)

```java
a = [ 
	"a",
	"b",
	"c"
];
a.each( ( Any element, Any index, Any array ) => {
	writeOutput( "#index#:#element#;" );
} );

```

Result: 1:a;2:b;3:c;

### Additional Examples

```java
aNames = array( "Marcus", "Sarah", "Josefine" );
arrayEach( aNames, ( Any element ) => {
	dump( element );
} );

```

[Run Example](https://try.boxlang.io/?code=eJxVkEFrwzAMhc%2FJr3jYFwc81rFblwbG2HmX3EoPrqO1pk5SbIWtjP33JdoY5PKhJ570hDK7xNjhRNwGf3kZp4FN9VS6ubcvC%2BWUnXkUemEnJOG78CQ8C4MqD%2FN0Su726vzZwFkYPA83UKSeBrYiwtDR528pXlTYNfgqi48UmN4mvk5soGo%2FdtRoceut%2Fluhsdfre%2FWhvhdrfUyNwnx%2FkSPR1eBhs1nktwWniSweF7UOaUd2EW3oaQtt1otxh7x8qNLoQ4whkx%2BHLv%2FH%2FAC9Els8)

```java
start = getTickCount();
a = [
	"a",
	"b",
	"c",
	"d",
	"e",
	"f",
	"g",
	"h",
	"i"
];
arrayEach( a, ( Any element, Any index, Any array ) => {
	writeOutput( "<code>#index#:#element# [#getTickCount()#]</code><br>" );
	sleep( 100 );
}, true, 3 );
writeOutput( "Total Time: #(getTickCount() - start)# milliseconds<br>" );

```

## Related

* [ArrayAppend](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayappend)
* [ArrayAvg](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayavg)
* [ArrayChunk](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraychunk)
* [ArrayClear](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayclear)
* [ArrayContains](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraycontains)
* [ArrayContainsNoCase](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraycontainsnocase)
* [ArrayDelete](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraydelete)
* [ArrayDeleteAt](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraydeleteat)
* [ArrayDeleteNoCase](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraydeletenocase)
* [ArrayEvery](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayevery)
* [ArrayFilter](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayfilter)
* [ArrayFind](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayfind)
* [ArrayFindAll](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayfindall)
* [ArrayFindAllNoCase](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayfindallnocase)
* [ArrayFindFirst](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayfindfirst)
* [ArrayFindNoCase](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayfindnocase)
* [ArrayFirst](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayfirst)
* [ArrayFlatMap](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayflatmap)
* [ArrayFlatten](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayflatten)
* [ArrayGetMetadata](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraygetmetadata)
* [ArrayGroupBy](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraygroupby)
* [ArrayIndexExists](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayindexexists)
* [ArrayInsertAt](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayinsertat)
* [ArrayIsDefined](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayisdefined)
* [ArrayLast](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraylast)
* [ArrayMap](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraymap)
* [ArrayMax](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraymax)
* [ArrayMedian](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraymedian)
* [ArrayMerge](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraymerge)
* [ArrayMid](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraymid)
* [ArrayMin](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraymin)
* [ArrayNew](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraynew)
* [ArrayNone](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraynone)
* [ArrayPop](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraypop)
* [ArrayPrepend](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayprepend)
* [ArrayPush](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraypush)
* [ArrayRange](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayrange)
* [ArrayReduce](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayreduce)
* [ArrayReduceRight](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayreduceright)
* [ArrayReject](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayreject)
* [ArrayResize](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayresize)
* [ArrayReverse](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayreverse)
* [ArraySet](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayset)
* [ArrayShift](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayshift)
* [ArraySlice](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayslice)
* [ArraySome](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraysome)
* [ArraySort](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraysort)
* [ArraySplice](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraysplice)
* [ArraySum](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraysum)
* [ArraySwap](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayswap)
* [ArrayToList](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraytolist)
* [ArrayToStruct](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraytostruct)
* [ArrayTranspose](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraytranspose)
* [ArrayUnique](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayunique)
* [ArrayUnshift](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayunshift)
* [ArrayZip](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayzip)
