Difference between Filter and Map in JavaScript31 Aug 2024 | 4 min read Overview:In JavaScript, both filter and map are higher-order functions that operate on arrays. However, they serve different purposes and have distinct functionalities. In this topic, we are going to discuss the difference between filter and map in JavaScript. Filter() method:A new array of all the elements that satisfy a specific condition is created by the filter() method. It accepts a callback function as a parameter, and each element of the array calls the callback function. Either true or false must be returned by the callback function. A component is added to the new array if the callback function for that component returns true. It does not include that element if it returns false. Example 1:Consider the situation where you have an array of integers and wish to make a new array that only contains even numbers. This is how to apply the filter method: Output [ 2, 4, 6 ] In this example, the callback function checks whether each element in the array is even by checking if the remainder of the division by 2 is zero. If the remainder is zero, the function returns true and that element is included in the new array. Example 2:Lets consider another example for filter method, and use the filter() technique to identify every left-footed player in the array. Output [ { name: 'Jasu', footedness: 'left', position: 'forward' }, { name: 'Loganadu', footedness: 'left', position: 'defense' } ] Map() method:The map() method calls a function for each array element and returns the results in a new array. The map method lets users manipulate the elements of an array however they see fit, returning the results of that modification in a brand-new array. Each element in the original array is changed by the map() method to produce a new array. The callback function that is passed to map(), like filter(), is invoked for each element of the array. The new value must be returned by the callback function in order to be included in the new array. Example:Consider the scenario where you wish to construct a new array from a set of numbers that contains the squares of each number. The map method can be applied as follows: Output [ 1, 4, 9, 16, 25 ] In this example, the callback function takes each element in the array and squares it, returning the new value. The map() method creates a new array containing the transformed elements. Example 2:Let's consider another example for map method to calculate the square roots of the ages contained within the array. Output [5, 6, 7, 8, 9] One key difference between filter() and map() is what they return. The filter() method always returns an array that contains a subset of the elements in the original array, based on some condition. In contrast, the map() method always returns a new array with the same number of elements as the original array, but with each element transformed according to the callback function. Another difference between filter() and map() is the type of the callback function. The callback function for filter() must return a boolean value, indicating whether or not the element should be included in the new array. The callback function for map() can return any value, and that value will become the new value of the corresponding element in the new array. It's worth noting that both filter() and map() do not modify the original array. Instead, they create a new array that is a subset or a transformation of the original array. ConclusionIn summary, filter() and map() are both useful array methods in JavaScript, but serve different purposes. The filter() method is used to create a new array with a subset of the original elements based on some condition, while map() is used to create a new array with each element transformed according to a callback function. Next TopicDifference between |
Introduction Have you ever experienced confusion regarding the appropriate usage of "do" and "make" in the English language? These two verbs are extensively utilized, yet their correct application can be perplexing. Although both verbs imply acting, the contexts in which they are employed can vary significantly. This article...
5 min read
Although they are often used synonymously, empathy and sympathy have distinct functions when building relationships. Empathy facilitates a closer, more intimate connection with their experiences than sympathy, which only makes it possible for us to understand and feel their suffering. Empathy is fundamentally the capacity to deeply...
5 min read
The Reserve Bank of India (RBI) is the main regulatory body in the Indian banking system, which divides banks into Scheduled and Non-Scheduled banks. This differentiation depends on a bank's compliance with particular standards and its listing in the Reserve Bank of India Act, 1934's Second...
6 min read
The big difference between bearer cheques and order cheques is that bearer cheques can be cashed by anyone who has them, while order cheques need the named payee's approval to be cashed or deposited. Around the globe, cheques are widely used as a handy way to pay....
6 min read
Memory allocation is an important aspect of computer programming, especially when it comes to creating and managing data structures. When writing code, memory is used to store variables and data, which can either be allocated statically or dynamically. In this article, we will explore the difference...
6 min read
An investigation is a planned process of looking into and gathering proof to find facts or settle a problem, usually one that has to do with the law or something important. On the other hand, inquiry is a less official way to look into something or find...
10 min read
In this article, we will discuss the difference between Log4js and Winston. Before discussing their differences, we must know about Log4js and Winston with their use cases, applications, and advantages. Introduction to Log4js Log4js is a logging library mainly used in Node.js applications. It will create a structured...
4 min read
Ecology and Ecosystem are two important terms that are frequently used in the study of environmental science. Despite their apparent similarity, they contain clear distinctions that are crucial to comprehend. We examine the distinction between ecology and ecosystem in this article. What is Ecology? Biology's field of ecology...
4 min read
A wide range of financial operations are included in banking, such as taking deposits from people and organizations and lending money to people who need it. The Reserve Bank of India (RBI) or other financial regulatory bodies have established specific regulations that govern these activities. Banks...
4 min read
Gulfs and straits are two common geographical features that are often found in close proximity to one another, and yet they differ in a number of important ways. Understanding the differences between gulfs and straits can help deepen our appreciation for the complexity and diversity of...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India