Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to convert a struct into an array without using a loop?

I'm curious, is there another way to convert a struct into an array in Coldfusion without looping over it? I know it can be done this way if we use a for in loop:

local.array = [];
for (local.value in local.struct)
{
   arrayAppend(local.array, local.value);
}
like image 753
Mohamad Avatar asked Apr 11 '26 13:04

Mohamad


2 Answers

Does StructKeyArray suit your requirements?

Description

Finds the keys in a ColdFusion structure.

like image 65
Antony Avatar answered Apr 13 '26 04:04

Antony


If you are trying to maintain order in your structure you could always use a Java LinkedHashMap like so:

cfmlLinkedMap = createObject("Java", "java.util.LinkedHashMap").init();

cfmlLinkedMap["a"] = "Apple";
cfmlLinkedMap["b"] = "Banana";
cfmlLinkedMap["c"] = "Carrot";

for(key in cfmlLinkedMap){
    writedump(cfmlLinkedMap[key]);  
}

You could also do the same thing in a more "java" way not sure why you'd want to but its always an option:

//no need to init
linkedMap = createObject("Java", "java.util.LinkedHashMap");

//java way
linkedMap.put("d","Dragonfruit");
linkedMap.put("e","Eggplant");
linkedMap.put("f","Fig");

//loop through values
iterator = linkedMap.entrySet().iterator();        

while(iterator.hasNext()){
    writedump(iterator.next().value);   
}

//or

//loop through keys
iterator = linkedMap.keySet().iterator();

while(iterator.hasNext()){
    writedump(linkedMap.get(iterator.next()));  
}

Just remember that the keys are case SeNsItIvE!

like image 35
bittersweetryan Avatar answered Apr 13 '26 05:04

bittersweetryan