# ArrayFilter

Filters an array and returns a new array containing the result This BIF will invoke the callback function for each item in the array, passing the item, its index, and the array itself.

* If the callback returns true, the item will be included in the new array.
* If the callback returns false, the item will be excluded from the new array.
* If the callback requires strict arguments, it will only receive the item and its index.
* If the callback does not require strict arguments, it will receive the item, its index, and the array itself.

## Parallel Execution

If the `parallel` argument is set to true, and no `max_threads` are sent, the filter 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 filter in parallel, and destroy it after the operation is complete. Please note that this may not be the most efficient way to filter, 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

```
ArrayFilter(array=[array], callback=[function:Predicate], parallel=[boolean], maxThreads=[any], virtual=[boolean])
```

### Arguments

| Argument     | Type                 | Required | Description                                                                                                                                                                                                                                                                                     | Default |
| ------------ | -------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------- |
| `array`      | `array`              | `true`   | The array to filter entries from                                                                                                                                                                                                                                                                |         |
| `callback`   | `function:Predicate` | `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 Predicate 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> |         |
| `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 numeric comparison

Take an array of struct items and use arrayFilter to return ones of a rating 3 and higher.

[Run Example](https://try.boxlang.io/?code=eJxt0MEKgkAQBuCz%2BxTDnhQkKOuSKHgx6mCQ3aLDRruypKtMa2Lhu7fbIchkbj8f%2FzAjsJU6QWQ9RHAC4ryI41BhUwproKxpSk59GyLTUhU2XRJn8P%2FohSkzYzuftDUyVfz1riZtZWg9potJepOdHMvASHIOiWCPGqXmqdV3cy2zV6ey1BxdEN8%2F%2BOBConowtAIPohjMEuS6RfXJZofkuM02EEcQhGQALySd7b22VePCLt9nOUfJSvnkpvZ3qWf1G9r8Zvs%3D)

```java
fruitArray = [ 
	{
		"fruit" : "apple",
		"rating" : 4
	},
	{
		"fruit" : "banana",
		"rating" : 1
	},
	{
		"fruit" : "orange",
		"rating" : 5
	},
	{
		"fruit" : "mango",
		"rating" : 2
	},
	{
		"fruit" : "kiwi",
		"rating" : 3
	}
];
favoriteFruits = arrayFilter( fruitArray, ( Any item ) => {
	return item.RATING >= 3;
} );
writedump( JSONSerialize( favoriteFruits ) );

```

Result: \[{"fruit":"apple","rating":4},{"fruit":"orange","rating":5},{"fruit":"kiwi","rating":3}]

### Using a member function

This is the same example as above, but using a member function on the array instead of a standalone function.

[Run Example](https://try.boxlang.io/?code=eJxtz0ELgkAQBeCz%2ByuGPSmIUNYlUfBS1MEgu0WHjXZlSVeZ1sTC%2F95uhw4qc3t8vOEJbKVOEVkPMVyAOB%2FiOFTYlMIGKGuaklPfhsi0VIVNV8QZ%2FAm9MWVubBeztkamiknvetZWhtZjupylD9nJsQyNJNeICPaqUWq%2Btfpp1or%2F9EDIUnN0wYVU9WBQBR7ECZh65LpF9cuCU3reZztIYggjMoAXkc423tuqceGQH7Oco2SlfHMXRu88q79fCGTe)

```java
fruitArray = [ 
	{
		"fruit" : "apple",
		"rating" : 4
	},
	{
		"fruit" : "banana",
		"rating" : 1
	},
	{
		"fruit" : "orange",
		"rating" : 5
	},
	{
		"fruit" : "mango",
		"rating" : 2
	},
	{
		"fruit" : "kiwi",
		"rating" : 3
	}
];
favoriteFruits = fruitArray.filter( ( Any item ) => {
	return item.RATING >= 3;
} );
writedump( JSONSerialize( favoriteFruits ) );

```

Result: \[{"fruit":"apple","rating":4},{"fruit":"orange","rating":5},{"fruit":"kiwi","rating":3}]

### Additional Examples

[Run Example](https://try.boxlang.io/?code=eJxtz0ELgkAQBeDz7q8YPClIUNYlUfBieOkgdooOG62ypKtMayHhf2%2BMCFZkbo%2BPN7wSe2USRDFABGfg7M0ZS%2FNTVsAeHNF1tXR8ivKkyI4Hyracjf6MXYWms916wbUodDXr2y24hlhrs80Cu6uXslVAil9CXopni8rIdFr3oGViWpiq2kh0ofxv9sGFRA9AtAEPohjoAUrTo%2F5mq19zHEEQ8hG8kN%2F6pqMK%2BwHlH8T%2BVdU%3D)

```java
fruitArray = [ 
	{
		FRUIT : "apple",
		RATING : 4
	},
	{
		FRUIT : "banana",
		RATING : 1
	},
	{
		FRUIT : "orange",
		RATING : 5
	},
	{
		FRUIT : "mango",
		RATING : 2
	},
	{
		FRUIT : "kiwi",
		RATING : 3
	}
];
favoriteFruits = arrayFilter( fruitArray, ( Any item ) => {
	return item.RATING >= 3;
} );
dump( favoriteFruits );

```

[Run Example](https://try.boxlang.io/?code=eJxtz0ELgkAUBODz7q94eFIQoaxLouDF8NJB7BQdNtqVJV3ltRYS%2FvdeEYEicxs%2BBkZhr22KKAaI4QScvThjWXHMS9iBI7qulo5PVZGW%2BWFP3Yaz0Z%2BxizCUqVstuBaFqWZ72wXXEGunbL3Abvqppyokxc8RV%2BLRorYy%2B7y70zP1vxkoXVuJLriQmgEINeBBnABNo7Q9mm8X%2FDaTGMKIj%2BBF%2FNo3nQuzaerfsLhTuA%3D%3D)

```java
fruitArray = [ 
	{
		FRUIT : "apple",
		RATING : 4
	},
	{
		FRUIT : "banana",
		RATING : 1
	},
	{
		FRUIT : "orange",
		RATING : 5
	},
	{
		FRUIT : "mango",
		RATING : 2
	},
	{
		FRUIT : "kiwi",
		RATING : 3
	}
];
favoriteFruits = fruitArray.filter( ( Any item ) => {
	return item.RATING >= 3;
} );
dump( favoriteFruits );

```

## Related

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayfilter.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
