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
Method Signature
append(value=[any], merge=[boolean])Arguments
value
any
true
The element to append. Can be any type.
merge
boolean
false
If true, the value is assumed to be an array and the elements of the array are appended to the array. If false, the value is appended as a single element.
false
bxDump(label=[string], depth=[numeric], maxRows=[numeric], top=[numeric], expand=[boolean], abort=[boolean], output=[string], format=[string], showUDFs=[boolean])
Outputs the contents of a variable (simple or complex) of any type for debugging purposes to a specific output location.
The available output locations are: - buffer: The output is written to the buffer, which is the default location. If running on a web server, the output is written to the browser. - console: The output is printed to the System console. - Absolute File Path The output is written to a file with the specified absolute file path.
The output format can be either HTML or plain text.
The default format is HTML if the output location is the buffer or a web server or a file, otherwise it is plain text for the console.
Method Signature
bxDump(label=[string], depth=[numeric], maxRows=[numeric], top=[numeric], expand=[boolean], abort=[boolean], output=[string], format=[string], showUDFs=[boolean])Arguments
label
string
false
A custom label to display above the dump (Only in HTML output)
depth
numeric
false
The recursion depth to display when dumping nested collections. 1-based: -1 (default) is unlimited, 0 shows nothing, 1 shows the top level with no recursion, 2 recurses once, etc. (Only in HTML output)
maxRows
numeric
false
The maximum number of keys/rows/items to display per level of a collection, array, or query. 1-based: -1 (default) is unlimited, 0 shows nothing, 1 shows a single row, etc. (Only in HTML output)
top
numeric
false
Deprecated: use maxRows instead. When maxRows is not also passed, top's value is used as maxRows. Kept for backwards compatibility with existing BoxLang code. (Only in HTML output)
expand
boolean
false
Whether to expand the dump. Be default, we try to expand as much as possible. (Only in HTML output)
true
abort
boolean
false
Whether to do a hard abort the request after dumping. Default is false
false
output
string
false
The output format which can be "buffer", "console", or "{absolute file path}". The default is "buffer".
format
string
false
The format of the output to a filename. Can be "html" or "text". The default is according to the output location.
showUDFs
boolean
false
Show UDFs or not. Default is true. (Only in HTML output)
true
chunk(length=[integer])
Chunks the array into an array of arrays of the specified size.
The final chunk may be shorter if the array does not divide evenly.
numbers = [ 1, 2, 3, 4, 5 ];
numbers.chunk( 2 ); // [ [ 1, 2 ], [ 3, 4 ], [ 5 ] ]
Method Signature
chunk(length=[integer])Arguments
length
integer
true
The size of each chunk.
contains(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.
Method Signature
Arguments
value
any
true
The value to find or a closure to be used as a search function.
substringMatch
boolean
false
If true, the search will be a substring match. Default is false. This only works on simple values, not complex ones. For that just use a function filter.
false
containsNoCase(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.
Method Signature
Arguments
value
any
true
The value to find or a closure to be used as a search function.
substringMatch
boolean
false
If true, the search will be a substring match. Default is false. This only works on simple values, not complex ones. For that just use a function filter.
false
each(callback=[function:Consumer], parallel=[boolean], maxThreads=[any], ordered=[boolean], virtual=[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.
Method Signature
Arguments
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
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. If a boolean is provided it will be assigned to the virtual argument instead.
ordered
boolean
false
whether parallel operations should execute and maintain order
false
virtual
boolean
false
If true, the function will be invoked using virtual threads. Defaults to false. Ignored if parallel is false.
false
every(callback=[function:Predicate], parallel=[boolean], maxThreads=[any], virtual=[boolean])
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.
Method Signature
Arguments
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
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. If a boolean is provided it will be assigned to the virtual argument instead.
virtual
boolean
false
If true, the function will be invoked using virtual threads. Defaults to false. Ignored if parallel is false.
false
filter(callback=[function:Predicate], parallel=[boolean], maxThreads=[any], virtual=[boolean])
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
Arguments
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
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. If a boolean is provided it will be assigned to the virtual argument instead.
virtual
boolean
false
If true, the function will be invoked using virtual threads. Defaults to false. Ignored if parallel is false.
false
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.
Method Signature
Arguments
value
any
true
The value to find or a closure to be used as a search function.
substringMatch
boolean
false
If true, the search will be a substring match. Default is false. This only works on simple values, not complex ones. For that just use a function filter.
false
findFirst(callback=[function], defaultValue=[any], parallel=[boolean], maxThreads=[any], virtual=[boolean])
Return first item in array that matches the predicate function.
Method Signature
Arguments
callback
function
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.
defaultValue
any
false
The default value to use if the array is empty or no value is returned from the predicate function.
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
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. If a boolean is provided it will be assigned to the virtual argument instead.
virtual
boolean
false
If true, the function will be invoked using virtual threads. Defaults to false. Ignored if parallel is false.
false
findNoCase(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.
Method Signature
Arguments
value
any
true
The value to find or a closure to be used as a search function.
substringMatch
boolean
false
If true, the search will be a substring match. Default is false. This only works on simple values, not complex ones. For that just use a function filter.
false
flatMap(callback=[function:Function], parallel=[boolean], maxThreads=[any], virtual=[boolean])
Maps each element and flattens the result one level.
Method Signature
Arguments
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.
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. Ignored if parallel is false.
false
groupBy(callback=[function])
Returns a struct of keys returned from the predicate function and values of arrays of matching rows.
Method Signature
Arguments
callback
function
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.
hash(algorithm=[string], encoding=[string], numIterations=[integer])
Creates an algorithmic hash of an object
Method Signature
Arguments
algorithm
string
false
The supported java.security.MessageDigest algorithm (case-insensitive) or "quick" for an insecure 64-bit hash
MD5
encoding
string
false
Applicable to strings ( default "utf-8" )
utf-8
numIterations
integer
false
The number of iterations to re-digest the object ( default 1 );
1
map(callback=[function:Function], parallel=[boolean], maxThreads=[any], virtual=[boolean])
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
Arguments
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
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).
Method Signature
Arguments
array2
array
true
The second array to merge
leaveIndex
boolean
true
Set to true maintain value indexes - if two values have the same index it will keep values from array1
false
mid(start=[integer], length=[integer])
Extracts a sub array from an existing array.
Method Signature
Arguments
start
integer
true
The position to start the slice from. Negative values count from the end of the array.
1
length
integer
false
The number of elements to return. 0 (default) returns all elements from start to the end of the array.
0
none(callback=[function:Predicate], parallel=[boolean], maxThreads=[any], virtual=[boolean])
Used to iterate over an array and test whether NONE item meets the test callback.
This is the opposite of 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.
Method Signature
Arguments
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
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.
virtual
boolean
false
If true, the function will be invoked using virtual threads. Defaults to false. Ignored if parallel is false.
false
reduce(callback=[function:BiFunction], initialValue=[any])
Run the provided udf over the array to reduce the values to a single output
Method Signature
Arguments
callback
function:BiFunction
true
The function to invoke for each item. The function will be passed 3 arguments: the accumulator, the current item, and the current index. You can alternatively pass a Java BiFunction which will only receive the first 2 args. The function should return the new accumulator value.
initialValue
any
false
The initial value of the accumulator
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.
Method Signature
Arguments
callback
function:BiFunction
true
The function to invoke for each item. The function will be passed 3 arguments: the accumulator, the current item, and the current index. You can alternatively pass a Java BiFunction which will only receive the first 2 args. The function should return the new accumulator value.
initialValue
any
false
The initial value of the accumulator
reject(callback=[function:Predicate], parallel=[boolean], maxThreads=[any], virtual=[boolean])
Filters an array and returns a new array containing the result.
This is the inverse of ArrayFilter.
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 false, the item will be included in the new array.
If the callback returns true, 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
Arguments
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
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. If a boolean is provided it will be assigned to the virtual argument instead.
virtual
boolean
false
If true, the function will be invoked using virtual threads. Defaults to false. Ignored if parallel is false.
false
resize(size=[integer])
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.
Method Signature
Arguments
size
integer
true
The new minimum size of the array
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.
Method Signature
Arguments
defaultValue
any
false
The value to return when the array is empty. If omitted, an exception is thrown for an empty array.
slice(start=[integer], length=[integer])
Extracts a sub array from an existing array.
Method Signature
Arguments
start
integer
true
The position to start the slice from. Negative values count from the end of the array.
1
length
integer
false
The number of elements to return. 0 (default) returns all elements from start to the end of the array.
0
some(callback=[function:Predicate], parallel=[boolean], maxThreads=[any], virtual=[boolean])
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.
Method Signature
Arguments
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
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. If a boolean is provided it will be assigned to the virtual argument instead.
virtual
boolean
false
If true, the function will be invoked using virtual thread. Defaults to false. Ignored if parallel is false.
false
sort(sortType=[any], sortOrder=[string], localeSensitive=[boolean], callback=[function:Comparator])
Sorts array elements.
Method Signature
Arguments
sortType
any
false
Options are text, numeric, or textnocase
textnocase
sortOrder
string
false
Options are asc or desc
asc
localeSensitive
boolean
false
Sort based on local rules
false
callback
function:Comparator
false
Function to sort by
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.
Method Signature
Arguments
index
Integer
true
The initial position to remove or insert from
elementCountForRemoval
Integer
false
The number of elemetns to remove
0
replacements
array
false
An array of elements to insert
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:
roworfalse: Serializes the query as a top-level struct with two keys:columns(an array of column names) anddata(an array of arrays representing each row's data).columnortrue: Serializes the query as a top-level struct with three keys:rowCount(the number of rows),columns(an array of column names), anddata(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
Method Signature
Arguments
queryFormat
string
false
If the variable is a query, specifies whether to serialize the query by rows or by columns. Valid values are:
row same as false, column same as true, or struct. Defaults to row.
useSecureJSONPrefix
string
false
If true, the JSON string is prefixed with a secure JSON prefix. (Not implemented yet)
false
useCustomSerializer
boolean
false
If true, the JSON string is serialized using a custom serializer. (Not implemented yet)
pretty
boolean
false
If true, the JSON string is formatted with indentation and line breaks for readability. Defaults to false.
false
toSet(type=[string], delimiter=[string])
Convert a collection into a Set, deduplicating automatically.
Accepts an Array, a list-delimited String, an existing Set, a QueryColumn, an XML node, a bounded Range, or any value castable to a Set. When the value is already of the requested variant it is returned as-is; otherwise a new Set of the specified variant is created and populated.
Method Signature
Arguments
type
string
false
The backing variant: "default" / "hash" (HashSet), "linked" / "ordered" (LinkedHashSet), or "sorted" / "tree" (TreeSet). When omitted, the caster defaults to LINKED for ordered collections like Arrays.
delimiter
string
false
When value is a String, the list delimiter to split on. Defaults to ",".
,
zip(array2=[array], callback=[function], parallel=[boolean], maxThreads=[any], virtual=[boolean])
Returns a zipped array.
Method Signature
Arguments
array2
array
true
The second array to zip
callback
function
false
An optional callback function to receive the item and the current index from both zipped arrays and return a new value.
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. Ignored if parallel is false.
false
Last updated
Was this helpful?
