array

The primary array class in BoxLang.

This class wraps a Java List and provides additional functionality for BoxLang.

BoxLang indices are one-based, so the first element is at index 1, not 0.

Array Methods

append(value=[any], merge=[boolean])

Append a value to an array

Arguments:

Argument
Type
Required
Default

value

any

true

null

merge

boolean

false

false

avg()

Return length of array

clear()

Clear all items from array

contains(value=[any], substringMatch=[boolean])

Array finders and contains functions with and without case sensitivity.

Please note that "contain" methods return a boolean, while "find" methods return an index. If you use a function as the value, it will be used as a search closure or lambda. The signature of the function should be:

,

,
( value, index ) => {
return true; // if the value is found, else false
}
,

,

Example:

,

,
array = [ 1, 2, 3, 4, 5 ];
index = array.find( ( value, index ) -> {
return value == 3;
} );
,

,

We recommend you use BoxLang lambdas (,{@code ->},) for this purpose, so they only act upon the value and index without any side effects. They will be faster and more efficient.

Arguments:

Argument
Type
Required
Default

value

any

true

null

substringMatch

boolean

false

false

containsNoCase(value=[any], substringMatch=[boolean])

Array finders and contains functions with and without case sensitivity.

Please note that "contain" methods return a boolean, while "find" methods return an index. If you use a function as the value, it will be used as a search closure or lambda. The signature of the function should be:

,

,
( value, index ) => {
return true; // if the value is found, else false
}
,

,

Example:

,

,
array = [ 1, 2, 3, 4, 5 ];
index = array.find( ( value, index ) -> {
return value == 3;
} );
,

,

We recommend you use BoxLang lambdas (,{@code ->},) for this purpose, so they only act upon the value and index without any side effects. They will be faster and more efficient.

Arguments:

Argument
Type
Required
Default

value

any

true

null

substringMatch

boolean

false

false

delete(value=[any], scope=[string])

Delete first occurance of item in array case sensitive

Arguments:

Argument
Type
Required
Default

value

any

true

null

scope

string

false

one

deleteAt(index=[integer])

Delete item at specified index in array

Arguments:

Argument
Type
Required
Default

index

integer

true

null

deleteNoCase(value=[any], scope=[string])

Delete first occurance of item in array case sensitive

Arguments:

Argument
Type
Required
Default

value

any

true

null

scope

string

false

one

duplicate(deep=[boolean])

Duplicates an object - either shallow or deep

Arguments:

Argument
Type
Required
Default

deep

boolean

false

true

each(callback=[function:Consumer], parallel=[boolean], maxThreads=[integer], ordered=[boolean])

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.

Arguments:

Argument
Type
Required
Default

callback

function:Consumer

true

null

parallel

boolean

false

false

maxThreads

integer

false

null

ordered

boolean

false

false

equals(obj=[any])

Verifies equality with the following rules:

  • Same object

  • Super class

Arguments:

Argument
Type
Required
Default

obj

any

true

null

every(callback=[function:Predicate], parallel=[boolean], maxThreads=[integer])

Used to iterate over an array and test whether every item meets the test callback.

The function will be passed 3 arguments: the value, the index, and the array. You can alternatively pass a Java Predicate which will only receive the 1st arg. The function should return true if the item meets the test, and false otherwise. ,

, ,,Note:,, This operation is a short-circuit operation, meaning it will stop iterating as soon as it finds the first item that does not meet the test condition. ,

, ,

,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. This allows for efficient processing of large arrays, especially when the test function is computationally expensive or the array is large.

Arguments:

Argument
Type
Required
Default

callback

function:Predicate

true

null

parallel

boolean

false

false

maxThreads

integer

false

null

filter(callback=[function:Predicate], parallel=[boolean], maxThreads=[integer])

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.

Arguments:

Argument
Type
Required
Default

callback

function:Predicate

true

null

parallel

boolean

false

false

maxThreads

integer

false

null

find(value=[any], substringMatch=[boolean])

This function searches the array for the specified value. Returns the index in the array of the first match, or 0 if there is no match.

Arguments:

Argument
Type
Required
Default

value

any

true

null

substringMatch

boolean

false

false

findAll(value=[any])

Return an array containing the indexes of matched values

Arguments:

Argument
Type
Required
Default

value

any

true

null

findAllNoCase(value=[any])

Return an array containing the indexes of matched values

Arguments:

Argument
Type
Required
Default

value

any

true

null

findNoCase(value=[any], substringMatch=[boolean])

Array finders and contains functions with and without case sensitivity.

Please note that "contain" methods return a boolean, while "find" methods return an index. If you use a function as the value, it will be used as a search closure or lambda. The signature of the function should be:

,

,
( value, index ) => {
return true; // if the value is found, else false
}
,

,

Example:

,

,
array = [ 1, 2, 3, 4, 5 ];
index = array.find( ( value, index ) -> {
return value == 3;
} );
,

,

We recommend you use BoxLang lambdas (,{@code ->},) for this purpose, so they only act upon the value and index without any side effects. They will be faster and more efficient.

Arguments:

Argument
Type
Required
Default

value

any

true

null

substringMatch

boolean

false

false

first()

Return first item in array

getMetadata()

Gets metadata for items of an array and indicates the array type.

hash(algorithm=[string], encoding=[string], numIterations=[integer])

Creates an algorithmic hash of an object

Arguments:

Argument
Type
Required
Default

algorithm

string

false

MD5

encoding

string

false

utf-8

numIterations

integer

false

1

indexExists(index=[any])

Returns whether there exists an item in the array at the selected index.

Arguments:

Argument
Type
Required
Default

index

any

true

null

insertAt(position=[integer], value=[any])

Append a value to an array

Arguments:

Argument
Type
Required
Default

position

integer

true

null

value

any

true

null

isDefined(index=[any])

Returns whether there exists an item in the array at the selected index.

Arguments:

Argument
Type
Required
Default

index

any

true

null

isEmpty()

Determine whether a given value is empty.

We check for emptiness of anything that can be casted to: Array, Struct, Query, or String.

join(delimiter=[String], initialValue=[any])

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

Arguments:

Argument
Type
Required
Default

delimiter

String

false

,

initialValue

any

false

null

last()

Return first item in array

len()

Returns the absolute value of a number

map(callback=[function:Function], parallel=[boolean], maxThreads=[integer])

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

Arguments:

Argument
Type
Required
Default

callback

function:Function

true

null

parallel

boolean

false

false

maxThreads

integer

false

null

max()

Get the max value from an array

median()

Return the median value of an array.

Will only work on arrays that contain only numeric values.

merge(array2=[array], leaveIndex=[boolean])

This function creates a new array with data from the two passed arrays.

To add all the data from one array into another without creating a new array see the built in function ArrayAppend(arr1, arr2, true).

Arguments:

Argument
Type
Required
Default

array2

array

true

null

leaveIndex

boolean

true

false

mid(start=[integer], length=[integer])

Extracts a sub array from an existing array.

Arguments:

Argument
Type
Required
Default

start

integer

true

1

length

integer

false

0

min()

Return length of array

none(callback=[function:Predicate], parallel=[boolean], maxThreads=[integer])

Used to iterate over an array and test whether NONE item meets the test callback.

This is the opposite of ,{@link ArraySome},. ,

, The function will be passed 3 arguments: the value, the index, and the array. You can alternatively pass a Java Predicate which will only receive the 1st arg. The function should return true if the item meets the test, and false otherwise. ,

, ,,Note:,, This operation is a short-circuit operation, meaning it will stop iterating as soon as it finds the first item that does meet the test condition. ,

, ,

,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. This allows for efficient processing of large arrays, especially when the test function is computationally expensive or the array is large.

Arguments:

Argument
Type
Required
Default

callback

function:Predicate

true

null

parallel

boolean

false

false

maxThreads

integer

false

null

parallelStream()

Returns a parallel stream of the array

pop(defaultValue=[any])

Remove last item in array and return it

Arguments:

Argument
Type
Required
Default

defaultValue

any

false

null

prepend(value=[any])

Append a value to the start an array

Arguments:

Argument
Type
Required
Default

value

any

true

null

push(value=[any])

Adds an element or an object to the end of an array, then returns the size of the modified array.

Arguments:

Argument
Type
Required
Default

value

any

true

null

range(to=[numeric])

Build an array out of a range of numbers or using our range syntax: {start}..{end} or using the from and to arguments

, You can also build negative ranges ,

,

,

,
arrayRange( "1..5" )
arrayRange( "-10..5" )
arrayRange( 1, 500 )
,

Arguments:

Argument
Type
Required
Default

to

numeric

false

null

reduce(callback=[function:BiFunction], initialValue=[any])

Run the provided udf over the array to reduce the values to a single output

Arguments:

Argument
Type
Required
Default

callback

function:BiFunction

true

null

initialValue

any

false

null

reduceRight(callback=[function:BiFunction], initialValue=[any])

This function iterates over every element of the array and calls the closure to work on that element.

It will reduce the array to a single value, from the right to the left, and return it.

Arguments:

Argument
Type
Required
Default

callback

function:BiFunction

true

null

initialValue

any

false

null

resize(size=[any])

Resets an array to a specified minimum number of elements.

This can improve performance, if used to size an array to its expected maximum. For more than 500 elements, use arrayResize immediately after using the ArrayNew BIF.

Arguments:

Argument
Type
Required
Default

size

any

true

null

reverse()

Returns an array with all of the elements reversed.

The value in [0] within the input array will then exist in [n] in the output array, where n is the amount of elements in the array minus one.

set(start=[any], end=[any], value=[any])

In a one-dimensional array, sets the elements in a specified index range to a value.

Useful for initializing an array after a call to arrayNew.

Arguments:

Argument
Type
Required
Default

start

any

true

null

end

any

true

null

value

any

true

null

shift(defaultValue=[any])

Removes the first element from an array and returns the removed element.

This method changes the length of the array. If used on an empty array, an exception will be thrown.

Arguments:

Argument
Type
Required
Default

defaultValue

any

false

null

slice(start=[integer], length=[integer])

Extracts a sub array from an existing array.

Arguments:

Argument
Type
Required
Default

start

integer

true

1

length

integer

false

0

some(callback=[function:Predicate], parallel=[boolean], maxThreads=[integer])

Used to iterate over an array and test whether ANY items meet the test callback.

The function will be passed 3 arguments: the value, the index, and the array. You can alternatively pass a Java Predicate which will only receive the 1st arg. The function should return true if the item meets the test, and false otherwise. ,

, ,,Note:,, This operation is a short-circuit operation, meaning it will stop iterating as soon as it finds the first item that meets the test condition. ,

, ,

,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 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. ,

Arguments:

Argument
Type
Required
Default

callback

function:Predicate

true

null

parallel

boolean

false

false

maxThreads

integer

false

null

sort(sortType=[any], sortOrder=[string], localeSensitive=[boolean], callback=[function:Comparator])

Sorts array elements.

Arguments:

Argument
Type
Required
Default

sortType

any

false

textnocase

sortOrder

string

false

asc

localeSensitive

boolean

false

null

callback

function:Comparator

false

null

splice(index=[Integer], elementCountForRemoval=[Integer], replacements=[array])

Modifies an array by removing elements and adding new elements.

It starts from the index, removes as many elements as specified by elementCountForRemoval, and puts the replacements starting from index position.

Arguments:

Argument
Type
Required
Default

index

Integer

true

null

elementCountForRemoval

Integer

false

0

replacements

array

false

null

stream()

Returns a stream of the array

sum()

Returns the sum of all values in an array

swap(position1=[any], position2=[any])

Swaps array values of an array at specified positions.

This function is more efficient than multiple assignment statements

Arguments:

Argument
Type
Required
Default

position1

any

true

null

position2

any

true

null

toJSON(queryFormat=[string], useSecureJSONPrefix=[string], useCustomSerializer=[boolean], pretty=[boolean])

Converts a BoxLang variable into a JSON (JavaScript Object Notation) string according to the specified options.

,Query Format Options,

, The ,,queryFormat,, argument determines how queries are serialized: ,

  • , ,

  • ,,row,, or ,,false,,: Serializes the query as a top-level struct with two keys: ,,columns,, (an array of column names) and ,,data,, (an array of arrays representing each row's data).,

  • , ,

  • ,,column,, or ,,true,,: Serializes the query as a top-level struct with three keys: ,,rowCount,, (the number of rows), ,,columns,, (an array of column names), and ,,data,, (a struct where each key is a column name and the value is an array of values for that column).,

  • , ,

  • ,,struct,,: Serializes the query as an array of structs, where each struct represents a row of data.,

  • , ,

,

,

,Usage,

,

,

,
// Convert a query to JSON
myQuery = ...;
json = jsonSerialize( myQuery, queryFormat="row" );
// Convert a list to JSON
myList = "foo,bar,baz";
jsonList = jsonSerialize( myList );
,

Arguments:

Argument
Type
Required
Default

queryFormat

string

false

row

useSecureJSONPrefix

string

false

false

useCustomSerializer

boolean

false

null

pretty

boolean

false

false

toList(delimiter=[String], initialValue=[any])

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

Arguments:

Argument
Type
Required
Default

delimiter

String

false

,

initialValue

any

false

null

toModifiable()

Convert an array, struct or query to its Modifiable counterpart.

toStruct()

Transform the array to a struct, the index of the array is the key of the struct

toUnmodifiable()

Convert an array, struct or query to its Unmodifiable counterpart.

unshift(object=[any])

This function adds one or more elements to the beginning of the original array and returns the length of the modified array.

Arguments:

Argument
Type
Required
Default

object

any

true

null

Examples

Last updated

Was this helpful?