
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.
Browser Compatibility
Syntax of JavaScript Filter
Parameters Definition
Let's try with a few of in-trusting examples.
Browser | Supports |
Chrome |
Yes |
IE |
Yes |
Firefox | Yes |
Safari | Yes |
Opera | Yes |
array.filter(function(item, index, arr), thisArgument);
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.
Amanda Martines 5 days ago
Exercitation photo booth stumptown tote bag Banksy, elit small batch freegan sed. Craft beer elit seitan exercitation, photo booth et 8-bit kale chips proident chillwave deep v laborum. Aliquip veniam delectus, Marfa eiusmod Pinterest in do umami readymade swag. Selfies iPhone Kickstarter, drinking vinegar jean.
ReplyAmanda Martines 5 days ago
Exercitation photo booth stumptown tote bag Banksy, elit small batch freegan sed. Craft beer elit seitan exercitation, photo booth et 8-bit kale chips proident chillwave deep v laborum. Aliquip veniam delectus, Marfa eiusmod Pinterest in do umami readymade swag. Selfies iPhone Kickstarter, drinking vinegar jean.
ReplyBaltej Singh 5 days ago
Drinking vinegar stumptown yr pop-up artisan sunt. Deep v cliche lomo biodiesel Neutra selfies. Shorts fixie consequat flexitarian four loko tempor duis single-origin coffee. Banksy, elit small.
ReplyMarie Johnson 5 days ago
Kickstarter seitan retro. Drinking vinegar stumptown yr pop-up artisan sunt. Deep v cliche lomo biodiesel Neutra selfies. Shorts fixie consequat flexitarian four loko tempor duis single-origin coffee. Banksy, elit small.
Reply