JavaScript Filter

JavaScript Filter

The filter function is very important in JavaScript, It’s likely used for the filter to Array in JavaScript if you got really understand this JavaScript function, then you almost reached your destination, It was introduced in edition five ECMAScript ES5 and most of the browser supported JavaScript filter, I will explore as well as going forward.

The filter is the JavaScript In-build function, which returns the new Array only if the condition passed, else returns an empty array.  

Table of  Content

JavaScript Filter Browser Compatibility 

Browser Supports

Chrome  

 Yes

IE

 Yes
Firefox Yes
Safari Yes
Opera Yes

JavaScript Filter Syntax

array.filter(function(item, index, arr), thisArgument);

JavaScript Filter Parameters Definition.

Parameters Definition
function(item, index, arr) callback function in the filter(), scans array and returns single or current items every iteration.
item Current items in every iteration.
index The current index of item (Optional)
arr The array of the object belongs to the current element.
thisArgument  The current Object (Optional)

Let’s try with a few of in-trusting examples.

Example 1: Find even numbers in an array.

 ES5 code syntax.

var arr = [1,20,4,23,45,67,89,34];  // TODO : Define Variable
var filterItems =  arr.filter(function(item){ // TODO : Get filter result as per condition.
  return items % 2 === 0;
});

E6 Code Syntax

let arr = [1,20,4,23,45,67,89,34];  // TODO : Define Variable
filterItems =  arr.filter((item)=>  item % 2 === 0); //TODO : Get filter result as per condition.

OutPut

[20,4,34];

Example 2: Find the name from array collection. ES 5 code syntax.

 E5 Code Syntax

 var arr =  [“gomehere”,”mukesh”,”eric”,”Dan”,”Lisa”];
   var filterItems =  arr.filter(function(item){
  return items.includes(“e”);
  }); 

 E6 Code Syntax

   let arr = [“gomehere”,”mukesh”,”eric”,”Dan”,”Lisa”];
   let filterItems =  arr.filter((item)=>  items.includes(“e”));

Output

 [“gomehere”,”mukesh”,”eric”] 

Want to know more example and something ask questions and feedback, Kindly do me a favor, inbox me on  gomehereapp@gmail.com, Also could contact me on my website social accounts as well. 

Author: admin

Leave a Reply

Your email address will not be published.