• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Dynamic Implicit Structure Creation Question

Contributor ,
Jul 10, 2014 Jul 10, 2014

Copy link to clipboard

Copied

Hello,

I was curious if it was possible to dynamically create an implicit structure where you might occasionally want to omit some keys from being added to the structure?

I played around with using ternary operators in the structure creation with no luck.

<cfset temp = {

     name = "Dave",

     (1 IS 1 ? "" : Job  =  "Superhero")

  } />

The only way I can see doing this now would be to create the structure and then use StructKeyDelete on the keys I want to remove OR build structures the traditional way and cfif my way around the keys I don't want to add."

Ex: <cfset structKeyDelete(temp,"Job") />

or

<cfset temp = structNew() />

<cfset temp.name = "Dave" />

<cfif 1 NEQ 1>

      <cfset temp.job = "Superhero" />

</cfif>

Does anyone know how to create dynamic keys in a structure while defining it implicitly?

Thanks!

Views

255

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jul 11, 2014 Jul 11, 2014

I can think of at least one reason why this fails. If there is just one key-value pair, there will be a comma stranded at the end.

You can of course tinker dynamically with the content of the keys of a struct and their respective values. However, a struct is an object, with set characteristics such as number of key-value pairs. So, it would be difficult to dynamically change the number of key-value pairs 'from within' the struct. Even if what you are trying to do were possible, it would be more c

...

Votes

Translate

Translate
Community Expert ,
Jul 11, 2014 Jul 11, 2014

Copy link to clipboard

Copied

I can think of at least one reason why this fails. If there is just one key-value pair, there will be a comma stranded at the end.

You can of course tinker dynamically with the content of the keys of a struct and their respective values. However, a struct is an object, with set characteristics such as number of key-value pairs. So, it would be difficult to dynamically change the number of key-value pairs 'from within' the struct. Even if what you are trying to do were possible, it would be more complex, and would certainly involve more code, than the ordinary alternative you mention.

In any case, here is a version of your original idea that works:

<cfset temp = (1 IS 2 ? {name="Dave"} : {name="Dave",Job="Superhero"})>

<cfdump var="#temp#">

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 11, 2014 Jul 11, 2014

Copy link to clipboard

Copied

LATEST

Thanks BKBK.  Yeah I saw the comma issue after I submitted the example.  However you are totally right that the amount of code needed to make the structure in this way would be way more than just removing the unwanted fields after the fact.   Thank you for the assistance.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation