Solved

coldfusion array filter

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now