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

Question about ArrayMap(), .map(), .filter(), and .reduce()

LEGEND ,
Jul 31, 2019 Jul 31, 2019

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,

^ _ ^

355
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 ,
Jul 31, 2019 Jul 31, 2019

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

Dave Watts, Eidolon LLC
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
LEGEND ,
Jul 31, 2019 Jul 31, 2019

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,

^ _ ^

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 ,
Jul 31, 2019 Jul 31, 2019

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.


/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
LEGEND ,
Aug 01, 2019 Aug 01, 2019

Never thought I'd see the day I say "Thank you for proving my assertion wrong."  LOL!

V/r,

^ _ ^

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 ,
Aug 01, 2019 Aug 01, 2019
LATEST

Glad I could help.


/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
Resources