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

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

local.array = [];
for (local.value in local.struct)
{
   arrayAppend(local.array, local.value);
}
+3
source share
3 answers

Does StructKeyArray meet your requirements?

Description

Finds keys in the ColdFusion structure.

+5
source

If you are trying to maintain order in your structure, you can always use the Java LinkedHashMap like this:

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

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

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

You can also do the same in more "java", but don’t know why you want, but this is 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 keys have the SeNsItIvE case!

+2

Coldfusion 10 Railo 4, ( ), Underscore.cfc library :

_ = new Underscore();// instantiate the library

valueArray = _.toArray({first: 'one', second: 'two'});// returns: ['one','two']

Note. Coldfusion structures are not ordered, so you are not guaranteed any particular order of values ​​in the resulting array.

(Disclaimer: I wrote Underscore.cfc)

+1
source

All Articles