Solved

coldfusion array filter

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Have you ever stumbled upon a software that is so great that you just love? It happened to me. Love at first sight. Filezilla Server.   Ok its not the most advanced ftp server I've came across. But its a fairly simple piece of software to get the …
Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

776 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