Filter method in JavaScript- How and when to use it?
Hey readers, in this article we will be discussing how to use the filter method in JavaScript in a step-wise, efficient manner. If you are a beginner and want to learn JavaScript then do check out the course on Codedamn.
Introduction
JavaScript is one of the most used languages when it comes to building web applications as it allows developers to wrap HTML and CSS code in it to make web apps interactive. It allows a web application to be interactive. It is also used for making animations on websites and has a large community on GitHub. JavaScript has tons of libraries, one of which is React which we will be covering in this article later.
Use cases:
- Building web server and its interactive functions
- Animations and graphics, adding special effects to web components
- Validating forms and exception errors
- Adding behavior and functionalities to web pages
Since JavaScript is a scripting language, it works without any libraries. Developers are free to write JavaScript language code anywhere and can still run it in the browser. There is no structure in JavaScript, the browser executes each line of the code from top to down.
Filter method
The arr.filter() method creates a new array from a given array that contains only those elements from the given array that satisfy the argument method’s criterion.
Also check: Find method in JavaScript
Parameters: As noted previously and as described below, this procedure accepts five parameters:
Syntax:
array.filter(callback(element, index, arr), thisValue)
Parameters: As noted before, this method accepts five parameters, which are listed below:
callback: The function that will be invoked for each element of the array is stored in this argument.
element: The value of the elements presently being processed is stored in this parameter.
index: This is an optional parameter that holds the index of the currentValue element in the array, starting at 0.
The whole array on which Array.every is called is stored in arr, which is an optional parameter.
ThisValue: This parameter is optional; it holds the context that will be provided as this during the callback function’s execution. If the context is given, it will be used in this way for each callback function invocation; otherwise, undefined will be used by default.
Return value: This method returns a new array containing only the elements that met the arg function’s requirement.
The arr.filter() method in JavaScript is demonstrated in the following examples:
Example 1: In this example, the method filter() creates a new array that only contains elements that satisfy the isPositive() function’s requirement.
function isPositive(value) {
return value > 0;
}
var filtered = [112, 52, 0, -1, 944].filter(isPositive);
print(filtered);
Array.filter() has the following advantages:
You don’t need to define an index because you’re working with an array element.
You don’t need to make a new array and manually add elements to it.
There is no need to establish a loop.
Always remember to return, or you’ll get an empty array.
To filter the object array based on attributes, use the filter() function in JavaScript. The filter() function returns a new array containing all of the array members that meet the supplied criteria. If no elements satisfy the requirement, the array is empty. Each array element is looped or iterated over by the filter() function, which then passes each element to the callback function.
Syntax:
var newArray = array.filter(function(item)
{
return conditional_statement;
});
The original array is unaffected by the filter() function.
Example 1: We establish an array of “students” and use the array’s filter() function to extract the members that satisfy the provided requirement.
<script>
var obj = {
'Students': [{
"name": "Rohan",
"Age":"15",
"RollNumber": "123",
"Marks": "99",
}, {
"name": "Ram",
"Age":"14",
"RollNumber": "223",
"Marks": "69",
},
{
"name": "Reena",
"Age":"13",
"RollNumber": "253",
"Marks": "89",
},
]
};
var newArray = obj.Students.filter(function (el)
{
return el.Age >=15 &&
el.RollNumber <= 200 &&
el.Marks >= 80 ;
}
);
console.log(newArray);
</script>
Conclusion
This was about the filter method in JavaSript using HTML, if you want to learn more about javascript, do check out the article and course on Codedamn of javascript along with the course. Hope you liked this, if you have any queries or suggestions do let us know in the comments.
If you are interested in learning JavaScript, do check out courses on codedamn with an in-built environment playground to learn and practice code. Join the community of codedamn and do check out other articles related to programming and development on codedamn and subscribe to our newsletter to never miss out on our new programs and updates.
If you have any queries or feedback do let us know in the comment section.
Sharing is caring
Did you like what Agam singh, Aman Ahmed Siddiqui, Aman Chopra, Aman, Amol Shelke, Anas Khan, Anirudh Panda, Ankur Balwada, Anshul Soni, Arif Shaikh, wrote? Thank them for their work by sharing it on social media.
No comments so far
Curious about this topic? Continue your journey with these coding courses: