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:
value
any
true
null
merge
boolean
false
false
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:
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:
value
any
true
null
substringMatch
boolean
false
false
delete(value=[any], scope=[string])
Delete first occurance of item in array case sensitive
Arguments:
value
any
true
null
scope
string
false
one
deleteAt(index=[integer])
Delete item at specified index in array
Arguments:
index
integer
true
null
deleteNoCase(value=[any], scope=[string])
Delete first occurance of item in array case sensitive
Arguments:
value
any
true
null
scope
string
false
one
duplicate(deep=[boolean])
Duplicates an object - either shallow or deep
Arguments:
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:
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:
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:
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:
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:
value
any
true
null
substringMatch
boolean
false
false
findAll(value=[any])
Return an array containing the indexes of matched values
Arguments:
value
any
true
null
findAllNoCase(value=[any])
Return an array containing the indexes of matched values
Arguments:
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:
value
any
true
null
substringMatch
boolean
false
false
hash(algorithm=[string], encoding=[string], numIterations=[integer])
Creates an algorithmic hash of an object
Arguments:
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:
index
any
true
null
insertAt(position=[integer], value=[any])
Append a value to an array
Arguments:
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:
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:
delimiter
String
false
,
initialValue
any
false
null
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:
callback
function:Function
true
null
parallel
boolean
false
false
maxThreads
integer
false
null
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:
array2
array
true
null
leaveIndex
boolean
true
false
mid(start=[integer], length=[integer])
Extracts a sub array from an existing array.
Arguments:
start
integer
true
1
length
integer
false
0
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:
callback
function:Predicate
true
null
parallel
boolean
false
false
maxThreads
integer
false
null
pop(defaultValue=[any])
Remove last item in array and return it
Arguments:
defaultValue
any
false
null
prepend(value=[any])
Append a value to the start an array
Arguments:
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:
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:
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:
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:
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:
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:
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:
defaultValue
any
false
null
slice(start=[integer], length=[integer])
Extracts a sub array from an existing array.
Arguments:
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:
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:
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:
index
Integer
true
null
elementCountForRemoval
Integer
false
0
replacements
array
false
null
swap(position1=[any], position2=[any])
Swaps array values of an array at specified positions.
This function is more efficient than multiple assignment statements
Arguments:
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:
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:
delimiter
String
false
,
initialValue
any
false
null
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:
object
any
true
null
Examples
Last updated
Was this helpful?