Solved

coldfusion array filter

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

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 …
Preface There are many applications where some computing systems need have their system clocks running synchronized within a small margin and eventually need to be in sync with the global time. There are different solutions for this, i.e. the W3…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

932 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

15 Experts available now in Live!

Get 1:1 Help Now