Working with Array.filter() method in JavaScript

by Jagadish Pulakhandam on 7/30/2012 1:35:28 PM
Rated 0 from 0 votes
Brief: Demonstrates on using Array.filter() method with/without an anonymous (callback) function
Posted to: Advanced JavaScript Programming/Development tutorials/videos - Arrays
Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

Attached source code/video demonstrates the following:
  • How to use Array.filter() method available in JavaScript
  • How to attach a callback function to Array.filter() method
  • How to attach an anonymous callback function to Array.filter() method
Source Code:

01.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
05.    <title></title>
06.    <script>
07.        //DEMO: using Callback function to work with Array.filter method
09.        //"Array.filter" method returns a new array that meet the condition specified in a callback function
11.        //The callback function need to take three arguments (value, index and array)
12.        function isEven(v, i, ary) {
13.            //return true if division by two is zero, else return false
14.            return (v % 2 == 0) ? true : false;
15.        }
17.        //define array
18.        var a = [2, 45, 26, 39, 12, 7, 9];
19.        //execute "filter" method by passing callback function
20.        var b  = a.filter(isEven); //"b" is new array with filtered elements from array "a"
21.        alert(b); //displays "2,6,12"
23.    </script>

Join the .NET Code Central Community and join the discussion!
Signing-up is FREE and quick. Do it now, we want to hear your opinion

Rated 0 from 0 votes ( login  to rate)
DotnetKicks DotnetKicksDe DotNetShoutout

Attachments / Source Code
You need to Login or Join for FREE to download the following

Video/Screen Recording (may not have audio narration/annotations)
You need to Login or Join for FREE to download the following