Whatever message this page gives is out now! Go check it out!
<cfscript>
structName=StructNew();
</cfscript><cfscript>
departments=StructNew();
</cfscript><cfscript>
departments = structNew("Ordered");
/** On iterating this struct, you get the values in insertion order, which is the way you inserted the values. **/
/** Create a structure and set its contents. **/
departments.John = "Sales";
departments.Tom = "Finance";
departments.Mike = "Education";
departments.Andrew = "Marketing";
/** Build a table to display the contents **/
writeOutput("<table cellpadding=""2"" cellspacing=""2"">
<tr>
<td><b>Employee</b></td>
<td><b>Department</b></td>
</tr>");
// Use cfloop to loop through the departments structure.The item attribute specifies a name for the structure key.
for ( person in departments ) {
writeOutput("<tr>
<td>#person#</td>
<td>#Departments[person]#</td>
</tr>");
}
writeOutput("</table>");
</cfscript>Employee | Department |
John | Sales |
Tom | Finance |
Mike | Education |
Andrew | Marketing |
<cfset myStruct = [:]> OR <cfset myStruct = [=]>departments = [Marketing = "John", Sales : [Executive : "Tom", Assistant = "Mike"]];<cfscript>
myStruct = {};
</cfscript><cfscript>
coInfo=StructNew();
coInfo.name="Adobe Systems Incorporated";
coInfo["name"]="Adobe Systems Incorporated";
coInfo={name="Adobe Systems Incorporated"};
</cfscript><cfscript>
coInfo={name="Adobe Systems Incorporated" industry="software"};
</cfscript><cfscript>
myStruct = {structKey1 = {innerStructKey1 = "innerStructValue1"}};
</cfscript><cfscript>
myStruct={structKey1.innerStructKey1 = "innerStructValue1"};
</cfscript><cfscript>
innerStruct1 = {innerStructKey1 = "innerStructValue1"};
myStruct1={structKey1 = innerStruct1};
</cfscript><cfscript>
i="coInfo";
"#i#"={name = "Adobe Systems Incorporated"};
</cfscript><cfscript>
writeDump( var={Name ="28 Weeks Later", Time = "7:45 PM"} );
</cfscript><cfscript>
student = {firstName="Jane", lastName="Janes", grades=[91, 78, 87]};
</cfscript><cfscript>
myStruct=StructNew();
myStruct.key1="A new structure with a new key";
WriteDump(myStruct);
myStruct.key2="Now I've added a second key";
WriteDump(myStruct);
</cfscript><cfscript>
departments=structnew();
departments.John = "Sales";
writeOutput("Before the first change, John was in the #departments.John# Department");
Departments.John = "Marketing";
writeOutput("After the first change, John is in the #departments.John# Department");
Departments["John"] = "Facilities";
writeOutput("After the second change, John is in the #departments.John# Department");
</cfscript>IsStruct(variable)StructCount(employee)StructIsEmpty(structure_name)StructKeyExists(structure_name, "key_name")if ( StructKeyExists(myStruct, "myKey") ) {
cfoutput( ) {
writeOutput("#mystruct.myKey#");
}
}<cfloop query="GetEmployees">
<cfif StructKeyExists(myStruct, LastName)>
<cfoutput>#LastName#: #mystruct[LastName]#</cfoutput><br>
</cfif>
</cfloop>IsDefined("structure_name.key")><cfscript>
temp=StructKeyList(structure_name, [delimiter]);
</cfscript><cfscript>
myStruct=StructNew();
myStruct.key1="Bugatti";
myStruct.key2="Lamborghini";
myStruct.key3="Maserati";
myStruct.key4="Ferrari";
myStruct.key5="Aprilia";
myStruct.key5="Ducati";
WriteOutput("The input struct is:");
WriteDump(myStruct);
WriteOutput("struct has " & StructCount(myStruct) & " keys: " & StructKeyList(myStruct) & "<br/>");
</cfscript><cfscript>
temp=StructKeyArray(structure_name);
</cfscript>Technique | Use |
Duplicate function | Makes a complete copy of the structure. All data is copied from the original structure to the new structure, including the contents of structures, queries, and other objects. As a result changes to one copy of the structure have no effect on the other structure.This function is useful when you want to move a structure completely into a new scope. In particular, if a structure is created in a scope that requires locking (for example, Application), you can duplicate it into a scope that does not require locking (for example, Request), and then delete it in the scope that requires locking. |
StructCopy function | Makes a shallow copy of a structure. It creates a structure and copies all simple variable and array values at the top level of the original structure to the new structure. However, it does not make copies of any structures, queries, or other objects that the original structure contains, or of any data inside these objects. Instead, it creates a reference in the new structure to the objects in the original structure. As a result, any change to these objects in one structure also changes the corresponding objects in the copied structure.The Duplicate function replaces this function for most, if not all, purposes. |
Variable assignment | Creates an additional reference, or alias, to the structure. Any change to the data using one variable name changes the structure that you access using the other variable name.This technique is useful when you want to add a local variable to another scope or otherwise change the scope of a variable without deleting the variable from the original scope. |
<cfscript>
myStruct = StructNew();
myNewStruct = StructNew();
myStruct.key1 = "The quick brown fox";
myStruct.key2 = "jumped over the";
myStruct.key3 = "lazy dog";
myNewStruct.k1 = "Atlantic";
myNewStruct.k2 = "Pacific";
myNewStruct.k3 = "Indian";
myArray[1]="North Island";
myArray[2]="South Island";
myStruct.key4 = myNewStruct;
// Assign myArray as a key to myNewStruct
myNewStruct.k4=myArray;
// Print myStruct
WriteOutput("The original struct is:");
WriteDump(myStruct);
// Copy the structure myStruct into a structure called copiedStruct
copiedStruct=StructCopy(myStruct);
// Print copiedStruct
WriteOutput("The copied struct is:");
WriteDump(copiedStruct);
</cfscript><cfscript>
myStruct = StructNew();
myNewStruct = StructNew();
myStruct.key1 = "The quick brown fox";
myStruct.key2 = "jumped over the";
myStruct.key3 = "lazy dog";
myNewStruct.k1 = "Atlantic";
myNewStruct.k2 = "Pacific";
myNewStruct.k3 = "Indian";
myArray[1]="North Island";
myArray[2]="South Island";
myStruct.key4 = myNewStruct;
// Assign myArray as a key to myNewStruct
myNewStruct.k4=myArray;
// Print myStruct
WriteOutput("The original struct is:");
WriteDump(myStruct);
// copy the structure myStruct into a structure called copiedStruct
copiedStruct=StructCopy(myStruct);
// Change copiedStruct properties
copiedStruct.n1="Alien";
copiedStruct.n2="Predator";
copiedStruct.n3="Terminator";
// Create a new struct anotherStruct
anotherStruct=StructNew();
anotherStruct.t1="Amazon";
anotherStruct.t2="Nile";
anotherStruct.t3="Danube";
// Assign anotherStruct as a new key to copiedStruct
copiedStruct.n4=anotherStruct;
// Print copiedStruct
WriteOutput("The changed struct,copiedStruct, is:");
WriteDump(copiedStruct);
// Create a duplicate version of copiedStruct
cloneStruct=Duplicate(copiedStruct);
// Print cloneStruct
WriteOutput("The new duplicate struct is:");
WriteDump(cloneStruct);
</cfscript>StructDelete(structure_name, key [, indicateNotExisting ])<cfscript>
myStruct=StructNew();
myStruct.item1="JPG";
myStruct.item2="BMP";
myStruct.item3="PNG";
// Print myStruct
WriteOutput("The input struct is:");
WriteDump(myStruct);
// Delete key "item1" from myStruct
StructDelete(myStruct,"item1");
// Print updated myStruct
WriteOutput("The modified struct is:");
WriteDump(myStruct);
</cfscript>StructClear(structure_name)<cfscript>
myStruct=StructNew();
myStruct.item1="JPG";
myStruct.item2="BMP";
myStruct.item3="PNG";
// Print myStruct
WriteOutput("The input struct is:");
WriteDump(myStruct);
// Create another struct
myAnotherStruct=StructNew();
// Copy myStruct to a new structure
myAnotherStruct=StructCopy(myStruct);
// Print myAnotherStruct
WriteOutput("The copied struct is:");
WriteDump(myAnotherStruct);
// Delete the structure myAnotherStruct
StructClear(myAnotherStruct);
// Print myAnotherStruct after deletion
WriteOutput("The deleted struct is:");
WriteDump(myAnotherStruct);
</cfscript><cfscript>
myStruct=StructNew();
myStruct.name1="Jeb";
myStruct.name2="Bernie";
myStruct.name3="Hillary";
myStruct.name4="Donald";
for ( i in StructSort(myStruct) ) {
cfoutput( ) {
writeOutput("#myStruct[i]#");
}
}
</cfscript><cfscript>
myStruct=StructNew();
myStruct.number1=75;
myStruct.number2=1112;
myStruct.number3=-674;
myStruct.number4=12;
myStruct.number5=3456;
myStruct.number6=-342;
myStruct.number7=3.14;
for ( i in StructSort(myStruct,"numeric") ) {
cfoutput( ) {
writeOutput("#myStruct[i]#");
}
}
</cfscript><cfscript>
i = "coInfo";
myStruct = {structKey1 = {innerStructKey1 = "innerStructValue1"}};
myStruct={structKey1.innerStructKey1 = "innerStructValue1"};
"#i#"={name = "Adobe Systems Incorporated"};
//you can also use a colon
writeOutput("You can also use a colon:");
myStruct={structKey1.innerStructKey1 : "innerStructValue1"};
</cfscript>mystruct = StructNew("casesensitive")<cfscript>
animals=StructNew("casesensitive")
animals.Aardwolf="Proteles cristata"
animals.aardvark="Orycteropus afer"
animals.Alligator="Mississippiensis"
animals.albatross="Diomedeidae"
writeDump(animals)
</cfscript>mystruct=${
"key1":"val1",
"key":"val2"
}<cfscript>
animals=${
Aardwolf:"Proteles cristata",
aardvark:"Orycteropus afer",
alligator:"Mississippiensis",
Albatross:"Diomedeidae"
}
writeDump(animals)
</cfscript>mystruct = StructNew("ordered-casesensitive")<cfscript>
animals=StructNew("ordered-casesensitive")
animals.Aardwolf="Proteles cristata"
animals.aardvark="Orycteropus afer"
animals.alligator="Mississippiensis"
animals.Albatross="Diomedeidae"
writeDump(animals)
</cfscript>mystruct=$[
"key1":"val1",
"key2":"val2"
]<cfscript>
animals=$[
aardwolf:"Proteles cristata",
aardvark:"Orycteropus afer",
alligator:"Mississippiensis",
albatross:"Diomedeidae"
]
writeDump(animals)
writeDump(animals.getMetadata())
</cfscript><cfscript>
obj1 =${ key1: 'val1', x: 25 };
obj2 =${ key2: 'val2', y: 50 };
mergedObj = $[obj1, obj2, "key3":"val3",z:75]
writeDump(mergedObj);
</cfscript><cfscript>
obj1 ={ foo: 'bar', x: 42 };
obj2 ={ foo: 'baz', y: 13 };
mergedObj = {...obj1, ...obj2, "key1":"23"};
writeDump(mergedObj);
</cfscript>({key1, key2, ............, keyN} = {key1:value1, key2:value2, , keyN:valueN})<cfscript>
val1=10 val2=20
writeOutput(val1)
writeOutput(val2)
</cfscript><cfscript>
[val1,val2]=[10,20]
writeOutput(val1)
writeOutput(val2)
</cfscript><cfset [foo, [[bar], baz]] = [61,[[42], 23]]>
<cfoutput>#bar#</cfoutput>
<cfoutput>#baz#</cfoutput><cfscript>
person = {
name: 'John Doe',
age: 25,
location: {
country: 'Canada',
city: 'Vancouver',
coordinates: [49.2827, -123.1207]
}
};
({name, location: {country, city, coordinates: [lat, lng]},age=40} = person)
</cfscript><cfscript>
person = {
name: 'John Doe',
age: 25,
location: {
country: 'Canada',
city: 'Vancouver',
coordinates: [49.2827, -123.1207]
}
};
({name, location: {country, city, coordinates: [lat, lng]},age} = person)
writeoutput(name & "<br>");
writeoutput(city & "<br>")
writeoutput(lat & "<br>");
writeoutput(lng & "<br>");
writeoutput(country & "<br>");
writeoutput(age)
</cfscript><cfscript>
a = 'foo';
b = 2021;
c = {};
o = {a, b, c}
writeOutput((o.a === 'foo'));
writeOutput((o.b === 2021));
writeDump((o.c));
</cfscript><cfscript>
CF9 = 'Centaur'
CF10 = 'Zeus'
CF11 = 'Splendor'
CF2016 = 'Raijin'
CF2018 = 'Aether'
CF2021 = 'Project Stratus'
CFReleaseCodeNames = ${
CF9,
CF10,
CF11,
CF2016,
CF2018,
CF2021
}
writeDump(CFReleaseCodeNames);
</cfscript><cfscript>
user = {
id: 45,
displayName: 'jdoe',
fullName: {
firstName: 'John',
lastName: 'Doe'
}
}
function userId({id}) {
return id;
}
writeoutput(userId(user)); // 45
</cfscript><cfscript>
writeOutput("Using destructuring in function parameter for lambda functions<br>");
myfunc=({id}) =>{ return id;}
user= {
id: 42,
displayName: 'jdoe',
fullName: {
firstName: 'John',
lastName: 'Doe'
}
}
result=myfunc(user)
writeoutput("User ID :" & result) // 42
</cfscript><cfscript>
writeOutput("Using destructuring in function parameter for closure functions<br>");
myfunc=function({id}){
return id;
};
user= {
id: 42,
displayName: 'jdoe',
fullName: {
firstName: 'John',
lastName: 'Doe12'
}
}
result=myfunc(user)
writeOutput("User ID :" & result) // 42
</cfscript><cfscript>
writeOutput("Destructuring objects in function parameter with default values <br>")
function greet ({greeting, name, time = 'today'}) {
return "#greeting# #name#! How are you #time#?"
}
// returns "Hello George! How are you today?"
message = greet({
name: 'George',
greeting: 'Hello'
});
writeoutput(message) // Hello George! How are you today?
</cfscript><cfscript>
writeoutput("Destructuring objects in function parameter with default values <br>")
function myFunc({name = 'Default user', age='N/A'} ) {
writeoutput("Name:" & name & "<br>")
writeoutput("Age:" & age & "<br>")
}
mystruct=[age:21]
myFunc()
myFunc(mystruct)
myFunc(["name":"John"])
</cfscript><cfscript>
user = {
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43,
'company': 'xyz'
}
writeoutput("Destructuring objects in function parameter with rest<br>")
function logDetails({name, age,...rest}) {
writeoutput(name & " is " & age & " year(s) old!")
writedump(rest)
}
logDetails(user)
</cfscript><cfscript>
writeoutput("Destructuring in function parameters with renaming the property<br>")
function myFunc({someLongPropertyName: prop}) {
writeoutput("Argument 1:" & prop & "<br>");
}
myFunc({someLongPropertyName: 'Hello'})
</cfscript><cfscript>
writeoutput("Destructuring in function parameters with renaming the property and default value<br>")
function myFunc({someLongPropertyName: prop = 'Default string'} ) {
writeoutput(prop & "<br>");
}
myfunc({someLongPropertyName: 'Hello'});
myfunc()
</cfscript><cfscript>
user = {
id: 45,
displayName: 'jdoe',
fullName: {
firstName: 'John',
lastName: 'Doe'
}
}
function whoisfirst({displayName, fullName: {firstName: name}}) {
return name;
}
function whois({displayName, fullName: {firstName: name,lastName: ln}}) {
writeOutput(ln);
return displayName;
}
function whoislast({displayName, fullName: {lastName: name}}) {
return name;
}
writeoutput(whois(user));
writeoutput(whoisfirst(user));
writeoutput(whoislast(user));
</cfscript><cfscript>
function greet ({greeting, name, time = 'today'}) {
return greeting & '<br>' & name & '!<br>' & 'How are you ' & time '?';
}
message = greet({
name: 'George',
greeting: 'Hello'
});
writeOutput(message);
</cfscript>