sg552
sg552

Reputation: 1543

Equivalent of StructKeyList() for struct value

StructKeyList() will give me list of struct key with comma delimited. Now I need to get struct value with comma delimited. Right now this is what I'm doing to get value

<cfloop collection="#form#" item="key" >
    #form[key]#,
</cfloop>

How can I get list of value from struct without loop? Thanks in advance.

Upvotes: 2

Views: 1722

Answers (2)

Shawn
Shawn

Reputation: 4786

Since you're using CF2016, if you want to avoid a loop, you can always use one of the higher-order functions like reduce().

fields = formScope.reduce( function(result, key, value) { 
    result.append(value) ;
    return result ;
}, [] ) ; 

This takes the struct of your form scope (formscope) and uses reduce() to step through it and take it down to a single value (which is the struct values turned into an array). Then we make the returned array into a list.

writeDump( fields.toList() )

My full test code is at https://trycf.com/gist/f00cc62cd4631f44070faf8008e6788f/acf2016?theme=monokai

<cfscript>
formScope = { 
    empty1 : "" ,
    fieldl : "text1" ,
    field2 : "text2" ,
    empty2 : "" ,
    field3 : "text3" ,
    field4 : "text4" ,
    empty3 : ""
} ; 

fields = formScope?.reduce( function(result, key, value) { 
    len(value) ? result.append(value) : "" ;
    return result ;
}, [] ) ; 

writeDump( fields?.toList() ?: "Form doesn't exist." ) ; 
</cfscript>

Giving us: text2,text3,text4,text1.

formScope is my simulated version of the form fields that would be passed to this page. I use mostly the member function versions of StructReduce, ArrayAppend and ArrayToList. I also use the initialVal optional parameter to initialize the reduction's result value as an array. I check that the value has a length (I could also trim if needed) before I insert a row in the array, allowing me to remove empty elements from my final list. I also use the safe navigation operator (?.) to do some basic validation to make sure the elements exist (like if the form didn't pass or the reduction produced invalid results) and to make it more error-resistant.

NOTE: I believe that can be taken back to be compatible with CF11, when ArrayReduce was introduced.

https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-s/structreduce.html http://ryanguill.com/functional/higher-order-functions/2016/05/18/higher-order-functions.html https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-a-b/arraytolist.html

Upvotes: 2

Kannan.P
Kannan.P

Reputation: 1273

I go through your problem. As per my knowledge is not possible to get list of value in structure within single functions. We have to loop the key and get the value of each. But I can give a solution for to get struct value with comma delimited.

<cfset strNew = {"a":"10","b":20,"c":30}>

Here strNew is my sample structure.

 <cfset  myList = ''>
<cfloop collection="#strNew#" item="key" >
    <cfset myList = listappend(myList,structfind(strNew,key))>
</cfloop>
<cfdump var="#myList#" />

Here I've loop over the structure keys and find the value of an particular key and append that in to and list by using listappend and structfind functions.

So you no need to put like #structure[key]#,In your end of comma(,) is also added the last value of key too. For example your code should return 10,20,30,.

So you no need to do like that. use structfind and listappend you can avoid end of the comma also. Hope it's help you.

Upvotes: 3

Related Questions