0

I have the following query set:

        ID  SERVICEID   DURATION
        1   1           30
        2   1           60
        3   1           90
        4   1           120
        8   2           30
        7   2           60
        6   2           90
        5   2           120
        9   3           30
        10  3           60
        11  3           90
        13  4           30
        12  4           60
        14  5           30
        15  5           60
        16  5           90
        17  6           30
        19  7           30
        18  7           60
        20  8           30
        21  9           30
        22  10          30
        23  11          60
        24  12          60
        25  13          30

I need some sort of a set of arrays like this: ARRAY[SERVICEID] = [DURATION1, DURATION2, DURATION N]

        ARRAY[1] = [30, 60, 90, 120]
        ...
        ARRAY[4] = [30, 60]
        ...
        ARRAY[12] = [60]
        etc..
2
  • So what issue are you having? Also, realize if there are gaps in the service id values, the array will contain gaps as well. Commented May 4, 2022 at 2:28
  • The issue is how to create the array :( And yes, I do realize the gaps in the array... The thing is I need to bring this query results in useful form to JavaScript. Commented May 4, 2022 at 11:47

1 Answer 1

1

I figured it out, using Structure instead of Arrays:

    <cfset durStruct = {} >
    <cfloop query="durations">
      <cfif NOT StructKeyExists(durStruct,serviceid)>
        <cfset durStruct[serviceid] = [] >
      </cfif>
      <cfset ArrayAppend(durStruct[serviceid],duration) > 
    </cfloop>

And then in Javascript:

    <script type="text/javascript" language="JavaScript">
            <cfoutput>let durData = #serializeJson(durStruct)#;</cfoutput>
    </script>
Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget to encode chars like single quotes which will break the javascript. I don't recall offhand what functions CF10 has, but later versions have encodeForJavascript().

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.