ArrayMerge

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

ArrayMerge(array1=[array], array2=[array], leaveIndex=[boolean])

Arguments

Argument
Type
Required
Description
Default

array1

array

true

The first array to merge

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

Examples

Standard function syntax

Merge two arrays resulting in a single re-indexed array. All elements of both arrays are preserved.

Run Example

fruit = [ 
	"apple",
	"banana",
	"orange"
];
veggies = [
	"tomato",
	"carrot",
	"corn",
	"peas",
	"peppers"
];
healthyFoods = arrayMerge( fruit, veggies );
writeOutput( arrayToList( healthyFoods ) );

Result: apple,banana,orange,tomato,carrot,corn,peas,peppers

Member function syntax

Merge two arrays resulting in a single re-indexed array. All elements of both arrays are preserved.

Run Example

fruit = [ 
	"apple",
	"banana",
	"orange"
];
veggies = [
	"tomato",
	"carrot",
	"corn",
	"peas",
	"peppers"
];
healthyFoods = fruit.merge( veggies );
writeOutput( arrayToList( healthyFoods ) );

Result: apple,banana,orange,tomato,carrot,corn,peas,peppers

Example where leaveIndex parameter is true

Merge two arrays resulting in a single re-indexed array. Where the both arrays have elements in the same position, only values from the first array are included in the result. Valid using standard or member function syntax.

Note how the first three elements of the veggies array are not merged because the fruit array already has values for elements 1-3.

Run Example

fruit = [ 
	"apple",
	"banana",
	"orange"
];
veggies = [
	"tomato",
	"carrot",
	"corn",
	"peas",
	"peppers"
];
healthyFoods = arrayMerge( fruit, veggies, true );
writeOutput( arrayToList( healthyFoods ) );

Result: apple,banana,orange,peas,peppers

Additional Examples

aNames = array( 10412, 42, 33, 2, 999, 12769, 888 );
aNames2 = array( 33, "b", "c", "d", "e", "f", "g" );
dump( arrayMerge( aNames, aNames2 ) );
// member function
dump( aNames.merge( aNames2 ) );
dump( aNames.merge( aNames2, true ) );

Last updated

Was this helpful?