"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Utilizing reduce, map, and filter for Data Processing in React Project

Utilizing reduce, map, and filter for Data Processing in React Project

Published on 2024-11-08
Browse:442

Memanfaatkan reduce, map, dan filter untuk Pengolahan Data dalam React Project

In the approximately 5 years I have worked as a web developer, these 3 array functions are the ones I use most often to manage data and interact with arrays. For the React project itself, these 3 array functions are very powerful for data processing, here are more or less effective uses of these 3 functions.

Map for renderList

import React from 'react';

const users = ['Alice', 'Bob', 'Charlie'];

function UserList() {
  return (
    
    {users.map((user, index) => (
  • {user}
  • ))}
); } export default UserList;

Filters for conditional rendering

import React from 'react';

const users = ['Al', 'Bob', 'Charlie'];

function UserList() {
  const filteredUsers = users.filter(user => user.length > 3);

  return (
    
    {filteredUsers.map((user, index) => (
  • {user}
  • ))}
); } export default UserList;

Reduce to Compute Data

import React from 'react';

const products = [
  { id: 1, name: 'Laptop', price: 1500 },
  { id: 2, name: 'Phone', price: 800 },
  { id: 3, name: 'Tablet', price: 1200 }
];

function TotalPrice() {
  const totalPrice = products.reduce((acc, product) => acc   product.price, 0);

  return (
    

Total Price: ${totalPrice}

); } export default TotalPrice;

Combining map, filter, and reduce in React

import React from 'react';

const products = [
  { id: 1, name: 'Laptop', price: 1500, discount: 200 },
  { id: 2, name: 'Phone', price: 800, discount: 50 },
  { id: 3, name: 'Tablet', price: 1200, discount: 100 }
];

function DiscountedProducts() {
  const discountedProducts = products.filter(product => product.discount > 0);
  const totalDiscount = discountedProducts.reduce((acc, product) => acc   product.discount, 0);

  return (
    

Total Discount: ${totalDiscount}

    {discountedProducts.map(product => (
  • {product.name} - Discount: ${product.discount}
  • ))}
); } export default DiscountedProducts;

Conclusion

In React applications, map, filter, and reduce are not only tools for manipulating data, but also ways to render the UI dynamically and efficiently. By understanding and mastering these functions, we can create applications that are more modular, easy to read, and scalable. So, keep exploring and implementing these functions in our React projects for maximum results

Release Statement This article is reproduced at: https://dev.to/saepulmalik27/memanfaatkan-reduce-map-dan-filter-untuk-pengolahan-data-dalam-react-project-5gho?1 If there is any infringement, please contact [email protected] delete
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3