JS How-To

Difference between map and filter function in JS

map & Filter in JS
map & Filter in JS

In React, both the map() and filter() functions are used to manipulate arrays, but they serve different purposes. Here’s a detailed explanation of the differences between map() and filter() in React:

Purpose Of map() : It is used to transform an array by applying a function to each element of the array and returning a new array with the transformed elements. filter() is used to create a new array that contains only the elements of the original array that pass a specific test.

Return Value: map() always returns a new array of the same length as the original array, but with the transformed elements. filter() always returns a new array that contains only the elements of the original array that pass the test specified in the callback function.

Parameters: The map() function takes a callback function that accepts three arguments: the current value of the element being processed, the index of the element being processed, and the array being processed. The filter() function also takes a callback function that accepts the same three arguments as the map() function.

Usage: map() is commonly used to render a list of components in React. For example, if you have an array of items and you want to render a list of components for each item, you can use map() to transform the array of items into an array of components. On the other hand, filter() is commonly used to filter out specific elements from an array. For example, if you have an array of objects and you want to filter out all objects where a specific property has a certain value, you can use filter() to create a new array that contains only the objects that meet the criteria.

Example using map() :

const items = ['apple', 'banana', 'orange'];

const itemList = items.map((item) => {
  return <li>{item}</li>;
});

return (
  <div>
    <ul>{itemList}</ul>
  </div>
);

In this example, the map() function is used to transform an array of strings (items) into an array of React <li> elements (itemList). The map() function is called on the items array, and for each item in the array, a new <li> element is returned with the item’s value. The resulting itemList array is then rendered in the React component as an unordered list.

Example using filter() :

const items = [
  { name: 'apple', category: 'fruit' },
  { name: 'lettuce', category: 'vegetable' },
  { name: 'banana', category: 'fruit' },
  { name: 'carrot', category: 'vegetable' },
  { name: 'orange', category: 'fruit' },
];

const fruitItems = items.filter((item) => {
  return item.category === 'fruit';
});

return (
  <div>
    <h3>Fruit Items:</h3>
    <ul>
      {fruitItems.map((item) => {
        return <li>{item.name}</li>;
      })}
    </ul>
  </div>
);

In the above example, the filter() function is used to create a new array (fruitItems) that contains only the objects in the items array that have a category property with the value of ‘fruit‘. The filter() function is called on the items array, and for each object in the array, the category property is checked to see if it is ‘fruit‘. The resulting fruitItems array is then mapped using map() to create a list of React <li> elements with the names of the fruit items. The resulting list is then rendered in the React component.

Here is a real-world example for map(), slice() is one of the best combinations with map() to create pagination functionality. For example, if you have a list of blog posts and you want to show only the first 10 posts on the first page, you can use slice() to create a new array that contains only the first 10 posts and then use map() to transform that array into an array of React components.

In summary, map() is used to transform an array by applying a function to each element, while filter() is used to create a new array that contains only the elements of the original array that pass a specific test. Both functions are commonly used in React for different purposes, and understanding the differences between them is important for writing efficient and effective React code. These can help developers to write more efficient and concise code and are an important part of any React developer’s toolkit

Happy Coding 🙂

About the author

Vishwas

A coder who is trying to solve some problems via "Procoders", Software Engineer By Profession, WEB Enthusiast, Hobby Blogger. I love to share Tech Ideas and like to learn and explore new things and Technologies.

Add Comment

Click here to post a comment