StructAppend

Appends the contents of a second struct to the first struct either with or without overwrite

Method Signature

StructAppend(struct1=[structloose], struct2=[structloose], overwrite=[boolean])

Arguments

Argument
Type
Required
Description
Default

struct1

struct

true

The target struct which will be the recipient of the appending

struct2

struct

true

The struct containing the values to be appended

overwrite

boolean

false

Default true. Whether to overwrite existing values found in struct1 from the values in struct2

true

Examples

Append options to config struct (without overwrite flag)

Run Example

config = { 
	A : 0,
	B : 0
};
options = {
	B : 1,
	C : 1
};
structAppend( config, options, false );
writeOutput( JSONSerialize( config ) );

Result: {"A":0,"B":0,"C":1}

Append options to config struct (same, but using member function)

Run Example

config = { 
	A : 0,
	B : 0
};
options = {
	B : 1,
	C : 1
};
config.append( options, false );
writeOutput( JSONSerialize( config ) );

Result: {"A":0,"B":0,"C":1}

Append options to config struct (with overwrite flag)

Run Example

config = { 
	A : 0,
	B : 0
};
options = {
	B : 1,
	C : 1
};
structAppend( config, options );
writeOutput( JSONSerialize( config ) );

Result: {"A":0,"B":1,"C":1}

Creating a request context from form and url scopes

Demonstrates how to construct a Request Context (rc) that combines the values of the form and url scopes

Run Example

rc = {};
structAppend( rc, form );
structAppend( rc, url );
writeOutput( JSONSerialize( rc ) );

Polyfill for earlier versions

In older Boxlang version where this function is not supported yet, you can fall back to a native java method to achieve the same behavior except that it does not have the overwriteFlag.

Run Example

config = { 
	A : 0,
	B : 0
};
options = {
	B : 1,
	C : 1
};
config.putAll( options );
writeOutput( JSONSerialize( config ) );

Result: {"A":0,"B":0,"C":1}

Additional Examples

Run Example

animals = { 
	COW : "moo",
	PIG : "oink"
};
// Show current animals
Dump( label="Current animals", var=animals );
// Create a new animal
newAnimal = {
	CAT : "meow"
};
// Append the newAnimal to animals
StructAppend( animals, newAnimal );
// Show animals, now includes cat
Dump( label="Animals with cat added", var=animals );

Last updated

Was this helpful?