Copy link to clipboard
Copied
Are ArrayMap(), .map(), .filter(), and .reduce() only available in CFSCRIPT? In all my Google searching, I've never seen any examples in CF tags, only CFSCRIPT code.
V/r,
^ _ ^
Copy link to clipboard
Copied
I would REALLY be surprised if that's the case. But the way you're going to use most of those functions is most likely to be in CFSCRIPT, because it's a "JS-like" way to do stuff. And I don't know if you could embed an unnamed callback function in CFML the way you can in CFSCRIPT.
Dave Watts, Eidolon LLC
Copy link to clipboard
Copied
Apparently that is what I'm finding out. Even using CFSET, I'm getting no love in my attempts to CFTAG it.
Which is no big deal. As you have pointed out, the most likely way these would be used would be inside CFSCRIPT tags, anyway. I was just throwing stuff against the wall to see what would stick.
Thank you for your response.
V/r,
^ _ ^
Copy link to clipboard
Copied
Not so fast, fellers. 🙂 You can indeed use those functions in tags.
And while it’s true that an *unnamed* callback function won’t work in tags (I don’t think), I can say with confidence that a named one will—and that can be defined in cffunction (or script), then used in such functions.
I show an example of doing an arraymap in both script and tags, running at cffiddle here.
But to save you having to go there, and in case it may be better searched/found by folks in the future, here are the two variants.
The first is in fact code you wrote and offered, Wolf (here: https://coldfusion.adobe.com/2017/10/map-reduce-and-filter-functions-in-coldfusion/)
<cfscript>
animals=["cat","dog","horse","lion","tiger"];
arrayMap(animals,function(item,index){
writeOutput("Animal number " & index & " is a " & item & "<br />");
});
</cfscript>
And here it is in all its “tag glory”:
<cffunction name="map">
<cfargument name="item">
<cfargument name="index">
<cfoutput>Animal number #index# is a #item# <br /></cfoutput>
</cffunction>
<cfset animals=["cat","dog","horse","lion","tiger"]>
<cfset arrayMap(animals,map)>
And please, no comments from the tag-hating peanut gallery. That’s really not the point of this thread.
Copy link to clipboard
Copied
Never thought I'd see the day I say "Thank you for proving my assertion wrong." LOL!
V/r,
^ _ ^
Copy link to clipboard
Copied
Glad I could help.