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

Find value in array

Contributor ,
Oct 19, 2020 Oct 19, 2020

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#" /> 

 

Screen Shot 2020-10-19 at 10.43.07 PM.png

642
Translate
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 , Oct 20, 2020 Oct 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,arrayOfUSR
...
Translate
Community Expert ,
Oct 20, 2020 Oct 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>

 

Translate
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 ,
Oct 20, 2020 Oct 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

Translate
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 ,
Oct 20, 2020 Oct 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:Screen Shot 2020-10-20 at 10.37.54 AM.png

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

Translate
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
Community Expert ,
Oct 20, 2020 Oct 20, 2020

Use quotes.

<cfset myStruct={"label-info"=[{"catalog-number"="undefined",label={disambiguation="aka Parlophone UK","sort-name"="Parlophone"}}]}>

<cfoutput>	
	Catalog Number: #myStruct["label-info"][1]["catalog-number"]# <br>
	Sort name: #myStruct["label-info"][1]["label"]["sort-name"]# 
</cfoutput>
Translate
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 ,
Oct 21, 2020 Oct 21, 2020

Following your example above I tried:  

 

 

<strong>Label:</strong>  #results3.["label-info"][1]["label"]["name"]#<br>

 

and got an error.  Suggestions?

A CFML variable name cannot end with a &quot;.&quot; character.
The variable results3. ends with a &quot;.&quot; character. You must either provide an additional structure key or delete the &quot;.&quot; character.
Translate
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
Community Expert ,
Oct 21, 2020 Oct 21, 2020

You didn't follow my example.

Mine: myStruct[]

Yours: results3.[]

Translate
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
Community Expert ,
Oct 21, 2020 Oct 21, 2020

ghanna, it's not results3.[ but just results3[, as BKBK showed with his reference to myStruct[. A subtle but critical difference.

And if somehow that's "not it" alone, I would recommend that you would do better to try doing a CFDUMP (or writedump, for cfscript) of the various bits of that variable, to find where any error is. So try first:

 

<cfdump var="#results3#">

then:

<cfdump  var="#results3.["label-info"]#">

then:

<cfdump  var="#results3.["label-info"][1]#">

and so oneach time doing a cfabort after it, to see what it shows.  (Or you could go in the reverse direction, taking things OFF from the right until it "works".)

 

Either way, you will find whatever else is amiss. I'm offering this as a general suggestion in the interest of both maybe helping you and others reading along, or beyond this one need. 🙂

 

Hope the first suggestion was the answer you needed.


/Charlie (troubleshooter, carehart. org)
Translate
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
Community Expert ,
Oct 21, 2020 Oct 21, 2020

Doh, bkbk, our replies show being literally 2 seconds apart. That's a first--and you won the race! 


/Charlie (troubleshooter, carehart. org)
Translate
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 ,
Oct 21, 2020 Oct 21, 2020
LATEST

Thanks, Gentlemen! Those damn pesky periods and commas get me all the time. This particular project long term will update a database for us by pulling data from 3 different online music databases using their APIs but for now, I threw it up on my personal site and added a search form to it just for fun. Play around if you got a minute. garyhanna.me/musictrack.cfm - I still need to catch errors if they API's don't return info though.

 

Again as always, I appreciate your help Charlie and BKBK!

 

Gary

Translate
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
Community Expert ,
Oct 20, 2020 Oct 20, 2020

Please mark the correct answer. You may of course mark your own explanation as the correct answer. 🙂 

The key thing is that correct answers will help others by narrowing their search.

Translate
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