Skip to main content
WolfShade
Legend
July 31, 2019
Question

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

  • July 31, 2019
  • 1 reply
  • 445 views

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,

^ _ ^

    This topic has been closed for replies.

    1 reply

    Community Expert
    July 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
    WolfShade
    WolfShadeAuthor
    Legend
    July 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,

    ^ _ ^

    Charlie Arehart
    Community Expert
    Community Expert
    July 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)