3

I have this array of structures setup:

<cfset table_columns = [
    {name="Right Name", var_name="right_name",  searchable="true", sortable="true"},
    {name="Right Type", var_name="right_type", searchable="true", sortable="true"},
    {name="Right Description", var_name="right_descr", searchable="true", sortable="true"},
    {name="Edit", var_name = "editcol", searchable="false", sortable="false"}
]>

How would I loop through that? Here is an example of what I need to do (which is obviously not working):

<cfloop array="#table_columns#" index="data_index">
    {"sName": "#table_columns[data_index]['name']#", "sTitle": "#table_columns[data_index]['var_name']#", "bsearchable": "#table_columns[data_index]['searchable']#", "bsortable": "#table_columns[data_index]['sortable']#"},
</cfloop>

2 Answers 2

6

With an array loop, the index value is an element of the array, not a position. Meaning data_index is a structure. So you can output the keys as usual (with either structure or dot notation).

<cfloop array="#table_columns#" index="data_index">
    {"sName": "#data_index['name']#", "sTitle": "#data_index['var_name']#", "bsearchable": "#data_index['searchable']#", "bsortable": "#data_index['sortable']#"},
</cfloop>
Sign up to request clarification or add additional context in comments.

2 Comments

I am getting the error: Element 'mykey' is undefined in a CFML structure referenced as part of an expression.
@yoosafinpace - For new questions, you should open a separate thread. Be sure to include 1) a self-contained example and b) any error message(s).
1
<cfloop collection="#table_columns#" item="data_index">

This is an approximation taken from the CF docs:

http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-71a6.html

I've never had to do it, but hopefully that'll get you started.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.