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=[integer])

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

integer

false

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. If parallel is false, this argument is ignored.

Examples

Simple numeric comparison

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

Run Example

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

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

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

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 );

Last updated

Was this helpful?