AyanUpadhaya

react_code_Thu Feb 12 2026 03:24:35 GMT+0600 (Bangladesh Standard Time)

Feb 11th, 2026
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ```javascript
  2. const products = [
  3.   { id: 1, name: 'Laptop', price: 1200, category: 'Electronics', stock: 5 },
  4.   { id: 2, name: 'Smartphone', price: 800, category: 'Electronics', stock: 12 },
  5.   { id: 3, name: 'Coffee Maker', price: 150, category: 'Appliances', stock: 8 },
  6.   { id: 4, name: 'Desk Chair', price: 250, category: 'Furniture', stock: 15 },
  7.   { id: 5, name: 'Monitor', price: 300, category: 'Electronics', stock: 0 },
  8. ];
  9.  
  10. /**
  11.  * FILTER: Get only electronics that are in stock
  12.  */
  13. const availableElectronics = products.filter(product =>
  14.   product.category === 'Electronics' && product.stock > 0
  15. );
  16.  
  17. /**
  18.  * MAP: Create a list of formatted strings with a 10% discount price applied
  19.  */
  20. const productPriceLabels = products.map(product => {
  21.   const discountedPrice = (product.price * 0.9).toFixed(2);
  22.   return `${product.name} is now $${discountedPrice}`;
  23. });
  24.  
  25. /**
  26.  * REDUCE: Calculate the total inventory value
  27.  */
  28. const totalInventoryValue = products.reduce((accumulator, product) => {
  29.   return accumulator + (product.price * product.stock);
  30. }, 0);
  31.  
  32. /**
  33.  * CHAINED: Get the total cost of all electronics (Filter -> Map -> Reduce)
  34.  */
  35. const totalElectronicsCost = products
  36.   .filter(p => p.category === 'Electronics')
  37.   .map(p => p.price)
  38.   .reduce((sum, price) => sum + price, 0);
  39.  
  40. // Usage
  41. console.log('Available Electronics:', availableElectronics);
  42. console.log('Price Labels:', productPriceLabels);
  43. console.log('Total Inventory Value:', totalInventoryValue);
  44. console.log('Sum of Electronics Prices:', totalElectronicsCost);
  45. ```
Advertisement
Add Comment
Please, Sign In to add comment