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

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

LEGEND ,
Jul 31, 2019 Jul 31, 2019

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,

^ _ ^

Views

257

Translate

Translate

Report

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

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

Votes

Translate

Translate

Report

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

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,

^ _ ^

Votes

Translate

Translate

Report

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

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.


/Charlie (troubleshooter, carehart.org)

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

V/r,

^ _ ^

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

LATEST

Glad I could help.


/Charlie (troubleshooter, carehart.org)

Votes

Translate

Translate

Report

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
Documentation