Skip to content

Instantly share code, notes, and snippets.

@noedlm
Last active November 9, 2017 18:48
Show Gist options
  • Save noedlm/2943ab419ccfedac775ab9b47d9fc0a3 to your computer and use it in GitHub Desktop.
Save noedlm/2943ab419ccfedac775ab9b47d9fc0a3 to your computer and use it in GitHub Desktop.
Coldfusion: Perks of iterating through structures and arrays using .each()
array = ["car", "bike", "foot"];
array.each(function(item, index) {
writeOutput(item & " at index: " & index);
});
structEach(struct, function(key, value) {
writeOutput(key & " = " & value);
});
arrayEach(array, function(item, index) {
writeOutput(item & "at index: " & index);
});
struct = {
fieldA = "A",
fieldB = "B",
fieldC = "C"
};
struct.each(function(key, value) {
writeOutput(key & " = " & value);
});
json = getPayload();
courses = deserializeJson(json); //this returns an array of structs
maxCourseId = getMaxId();
courses.each(function(course, index)) {
importClass(course, maxCourseId + index);
}
function importClass(required struct course, required numeric index) {
queryObject = new query();
arguments.course.each(function(key, value) {
queryObject.addParam(name=key, value=value);
});
//build and execute insert/update query. Here is where we use arguments.index
}
json = getPayload();
courses = deserializeJson(json); //this returns an array of structs
for(course in courses) {
importClass(course);
}
function importClass(required struct course) {
queryObject = new query(datasource);
queryObject.addParam(name="fieldA", value=arguments.course["fieldA"]);
queryObject.addParam(name="fieldB", value=arguments.course["fieldB"]);
queryObject.addParam(name="fieldC", value=arguments.course["fieldC"]);
//build and execute insert/update query
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment