Solved

coldfusion array filter

Posted on 2015-01-30
2
237 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 500 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
Determining the an SCCM package name from the Package ID
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

705 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