Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

coldfusion array filter

Posted on 2015-01-30
2
Medium Priority
?
260 Views
Last Modified: 2015-01-30
Hello,

Below is Arrray Filter that I don't understand and would like to know if anyone can explain how it works and what does Function(n) do?

The help documentation does not explain it well.

Thank you!!!

<cfscript>
names = ["Ray","Adam","Scott","Todd","Dave"];
shortNames = arrayFilter(names, function(n) {
    return len(n) < 4;
});
writeDump(shortNames);
</cfscript>

Open in new window

0
Comment
Question by:Rad1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 2000 total points
ID: 40581122
It's actually a neat feature.

how it works and what does Function(n) do?

Say for some bizarre reason you wanted to extract all elements in #names# that are 3 characters or less. How would you do it? You'd probably create an empty array to store the results.  Then loop through #names# and check the length of each element. If it's 3 chars or less, you'd add it to the result, right?
         
          results= []; // stores names under 4 chars
          for ( currName in names) {
                if ( len(currName) < 4 ) {
                      arrayAppend( results, currName );
                }
          }
          writeDump( results );

ArrayFilter provides a simpler way to do the same thing using closures. When you pass in an array, CF applies the logic inside Function(n) to each element. (The poorly named parameter "n" represents a single element in whatever array you pass in). The logic can be anything you want. The only rule is it should return "true" when you want to include the element in the result, and "false" when you want to exclude it.

In the case of the function above, the logic says "include the element if it has a length < 4".

 function( currentElement ) {
    // this returns true if the value is less than 4 chars long 
    return len(currentElement) < 4;
});

Open in new window


When you invoke arrayFilter, CF applies that function to each element:

      "Ray"   <=== returns true because it's 3 chars
      "Adam" <=== returns false, it's 4 chars
      "Scott" <=== returns false, it's 5 chars
      "Todd"  <=== returns false, it's 4 chars
      "Dave"  <=== returns false, it's 4 chars

.. so the result is:

      "Ray"

Again, the logic can be anything you want. It could just as easily say "only include elements that contain the letter 'A'":

function( currentElement ) {
    // this returns true if the value contains the letter "A"
    return currentElement contains "A";
}

Open in new window



CF would then apply that logic to each element:


      "Ray"   <=== returns true because it contains "A"
      "Adam" <=== returns true because it contains "A"
      "Scott" <=== returns false, no "a"
      "Todd"  <=== returns false, no "a"
      "Dave"  <=== returns true because it contains "A"

... and return:

      "Ray"
      "Adam"
      "Dave"

Hope that helps to explain how it works.
0
 

Author Closing Comment

by:Rad1
ID: 40581133
Excellent, yes very well.
Thank you
0

Featured Post

Looking for a new Web Host?

Lunarpages' assortment of hosting products and solutions ensure a perfect fit for anyone looking to get their vision or products to market. Our award winning customer support and 30-day money back guarantee show the pride we take in being the industry's premier MSP.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
If, like me, you have a lot of Dell servers in the estate you manage this article should save you a little time. When attempting to login to iDrac on any server I would be presented with two errors. The first reads "Do you want to run this applicati…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question