Skip to main content
Inspiring
October 20, 2020
Answered

Find value in array

  • October 20, 2020
  • 1 reply
  • 1042 views

Hello,

I need to find a value within an array. In the code example below I need to find ID when Country=US. In this code example, the result I need is found at results2.releases[5].ID however the order and number of results in the array vary depending on the JSON returned. How do I loop over the results and find the ID when country = US? 

 

As always appreciate any help!

Gary

<cfhttp method="get" url="http://musicbrainz.org/ws/2/recording/729cf505-94eb-4fbe-bc76-cbae44cff091?inc=artist-credits+isrcs+releases&fmt=json" result="httpResults2" throwonerror="Yes">
</cfhttp>
<cfset results2 = deserializeJSON(httpResults2.filecontent)>
<cfdump var="#results2#" /> 

 

    This topic has been closed for replies.
    Correct answer BKBK

    You could use ArrayFilter

     

    <cfscript>
    releasesArray=results2.releases;
    
    if ( arrayLen(releasesArray) gt 0 ) {
    	
    	arrayOfUSReleases=arrayFilter(releasesArray,function(item){
               return item.country eq 'US';
         });
         
      	/* writedump(arrayOfUSReleases);*/
       
     	/* Get the IDs. Note that each element of arrayOfUSReleases is a struct. */
    	arrayofUSReleaseIDs=arrayNew(1);
    	
    	for (index=1; index lte arrayLen(arrayOfUSReleases); index=index+1) {
    		arrayAppend(arrayofUSReleaseIDs,arrayOfUSReleases[index].id);
    	}
       
        writedump(arrayofUSReleaseIDs) ;
    }
    </cfscript>

     

    1 reply

    BKBK
    Community Expert
    BKBKCommunity ExpertCorrect answer
    Community Expert
    October 20, 2020

    You could use ArrayFilter

     

    <cfscript>
    releasesArray=results2.releases;
    
    if ( arrayLen(releasesArray) gt 0 ) {
    	
    	arrayOfUSReleases=arrayFilter(releasesArray,function(item){
               return item.country eq 'US';
         });
         
      	/* writedump(arrayOfUSReleases);*/
       
     	/* Get the IDs. Note that each element of arrayOfUSReleases is a struct. */
    	arrayofUSReleaseIDs=arrayNew(1);
    	
    	for (index=1; index lte arrayLen(arrayOfUSReleases); index=index+1) {
    		arrayAppend(arrayofUSReleaseIDs,arrayOfUSReleases[index].id);
    	}
       
        writedump(arrayofUSReleaseIDs) ;
    }
    </cfscript>

     

    ghanna1Author
    Inspiring
    October 20, 2020

    Thanks! The code work and I appreciate you commenting it out as you went along. I have zero experience writing scripts so your /comments/ at least allowed me to somewhat follow along. It's on my bucket list of things to learn. 

     

    Gary

    ghanna1Author
    Inspiring
    October 20, 2020

    One more quick question...

    I've tried all different [ ] placements to get CF not to see the dash in label-info as an operative. I've found a bunch of links when I search for help that refers to a CF8 document that now returns a 404. How do I get name out of:

    <cfset label = #results3.[label-info][1].label.name# />  --- my last attempt.