Course Hive
Search

Welcome

Sign in or create your account

Continue with Google
or
JavaScript array filter method
Play lesson

JavaScript Tutorial - JavaScript array filter method

4.0 (0)
13 learners

What you'll learn

This course includes

  • 12.3 hours of video
  • Certificate of completion
  • Access on mobile and TV

Summary

Keywords

Full Transcript

Link for all dot net and sql server video tutorial playlists http://www.youtube.com/user/kudvenkat/playlists Link for slides, code samples and text version of the video http://csharp-video-tutorials.blogspot.com/2014/12/javascript-array-filter-method.html Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help. https://www.youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?sub_confirmation=1 The filter() method creates a new array and populates that array with all the elements that meet the condition specified in a callback function. Syntax : array.filter(callbackFunction[, thisArg]) Parameters callbackFunction Required. Function that gets called for each element of the array. If the function returns true, the element is kept otherwise filtered. thisArg Optional. An object to which the this keyword can refer in the callbackfn function. The filter method calls the callbackfn function one time for each element in the array. If the callback function returns false for all elements of the array, the length of the new array that will be returned is 0. Callback Function Syntax function callbackFunction(value, index, array) Callback Function Parameters value The value of the element in the array index The index position of the element in the array array The source array object that contains the element Example 1 : Retrieve only even numbers from myArray // Callback function function IsEven(value, index, array) { if (value % 2 == 0) { return true; } else { return false; } } // Source array var myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // Pass the callback function as argument to the filter method var result = myArray.filter(IsEven); document.write(result); Output : 2,4,6,8,10 Example 2 : In Example 1 we defined a callback function first and then passed it as an argument to the filter() method. In the example below, we created the callback function as an anonymous function directly in the filter method where it is required. // Source array var myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // callback function created directly in the filter method as anonymous function var result = myArray.filter(function (v, i, a) { return v % 2 == 0 }); document.write(result); Output : 2,4,6,8,10 Example 3 : Remove duplicates from javascript array var myArray = ["Sam", "Mark", "Tim", "Sam"]; var uniqueItems = myArray.filter(function (v, i, a) { return a.indexOf(v) == i }); document.write(uniqueItems); Output : Sam,Mark,Tim

Course Hive

Continue this lesson in the app

Install CourseHive on Android or iOS to keep learning while you move.

Related Courses

FAQs

Course Hive
Download CourseHive
Keep learning anywhere