Mastering Higher Order Array Functions in JavaScript: A Developer’s Guide

higher order array functions
// A simple callback function  
function notify() {  
  console.log('Notification sent!');  
}  
  
// A higher-order function that takes another function as an argument  
function processTransaction(transaction, callback) {  
  console.log(`Processing transaction for ${transaction.amount} USD`);  
  callback(); // Executing the callback function  
}  
  
const transaction = { amount: 100 };  
processTransaction(transaction, notify);  
const products = [  
  { name: 'Laptop', price: 1000, quantity: 5 },  
  { name: 'Phone', price: 500, quantity: 10 },  
  { name: 'Tablet', price: 300, quantity: 15 }  
];  
  
// Calculate total stock value  
function calculateTotalStockValue(products) {  
  let total = 0;  
  for (let i = 0; i < products.length; i++) {  
    total += products[i].price * products[i].quantity;  
  }  
  return total;  
}  
  
// Filter low stock items  
function filterLowStockItems(products) {  
  const lowStockItems = [];  
  for (let i = 0; i < products.length; i++) {  
    if (products[i].quantity < 10) {  
      lowStockItems.push(products[i]);  
    }  
  }  
  return lowStockItems;  
}  
  
console.log(calculateTotalStockValue(products)); // 15500  
console.log(filterLowStockItems(products)); // [{ name: 'Laptop', price: 1000, quantity: 5 }]  
const products = [  
  { name: 'Laptop', price: 1000, quantity: 5 },  
  { name: 'Phone', price: 500, quantity: 10 },  
  { name: 'Tablet', price: 300, quantity: 15 }  
];  
  
// Higher order function to perform operations on products  
function processProducts(products, operation) {  
  return products.map(operation);  
}  
  
// Operations  
function calculateStockValue(product) {  
  return product.price * product.quantity;  
}  
  
function checkLowStock(product) {  
  return product.quantity < 10;  
}  
  
// Using the higher order function  
const stockValues = processProducts(products, calculateStockValue);  
const lowStockItems = products.filter(checkLowStock);  
  
console.log(stockValues); // [5000, 5000, 4500]  
console.log(lowStockItems); // [{ name: 'Laptop', price: 1000, quantity: 5 }]  
const orders = [  
  { id: 1, amount: 250 },  
  { id: 2, amount: 400 },  
  { id: 3, amount: 150 }  
];  
  
orders.forEach(order => {  
  console.log(`Order ID: ${order.id}, Amount: ${order.amount}`);  
});  
const salaries = [3000, 5000, 7000];  
const increasedSalaries = salaries.map(salary => salary * 1.1);  
console.log(increasedSalaries); // [3300, 5500, 7700]  
const employees = [  
  { name: 'Alice', age: 25 },  
  { name: 'Bob', age: 35 },  
  { name: 'Charlie', age: 30 }  
];  
  
const youngEmployees = employees.filter(employee => employee.age < 30);  
console.log(youngEmployees); // [{ name: 'Alice', age: 25 }]  
const expenses = [200, 300, 150];  
const totalExpenses = expenses.reduce((acc, expense) => acc + expense, 0);  
console.log(totalExpenses); // 650  
const customers = [  
  { name: 'Dave', purchaseAmount: 1200 },  
  { name: 'Eve', purchaseAmount: 800 },  
  { name: 'Frank', purchaseAmount: 1500 }  
];  
  
const highSpender = customers.find(customer => customer.purchaseAmount > 1000);  
console.log(highSpender); // { name: 'Dave', purchaseAmount: 1200 }  
const users = [  
  { name: 'A', age: 28, city: 'New York', email: 'a@example.com' },  
  { name: 'B', age: 22, city: 'Los Angeles', email: 'b@example.com' },  
  { name: 'C', age: 32, city: 'New York', email: 'c@example.com' },  
  { name: 'D', age: 24, city: 'Chicago', email: 'd@example.com' }  
];  
  
// Finding users from New York  
const newYorkUsers = users.filter(user => user.city === 'New York');  
console.log(newYorkUsers); // [{ name: 'A', age: 28, city: 'New York', email: 'a@example.com' }, { name: 'C', age: 32, city: 'New York', email: 'c@example.com' }]  
  
// Calculating average age  
const totalAge = users.reduce((acc, user) => acc + user.age, 0);  
const averageAge = totalAge / users.length;  
console.log(averageAge); // 26.5  
  
// Extracting emails  
const emails = users.map(user => user.email);  
console.log(emails); // ['a@example.com', 'b@example.com', 'c@example.com', 'd@example.com']  
const sales = [  
  { customer: 'Alice', amount: 300, region: 'North' },  
  { customer: 'Bob', amount: 700, region: 'South' },  
  { customer: 'Charlie', amount: 150, region: 'North' },  
  { customer: 'Dave', amount: 800, region: 'South' }  
];  
  
// Calculating total revenue  
const totalRevenue = sales.reduce((acc, sale) => acc + sale.amount, 0);  
console.log(totalRevenue); // 1950  
  
// Finding high-value sales (amount > 500)  
const highValueSales = sales.filter(sale => sale.amount > 500);  
console.log(highValueSales); // [{ customer: 'Bob', amount: 700, region: 'South' }, { customer: 'Dave', amount: 800, region: 'South' }]  
  
// Generating a list of customer names  
const customerNames = sales.map(sale => sale.customer);  
console.log(customerNames); // ['Alice', 'Bob', 'Charlie', 'Dave']
const numbers = [1, 2, 3, 4, 5];  
  
// Functions to double and square numbers  
const double = x => x * 2;  
const square = x => x * x;  
  
// Composing functions  
const compose = (f, g) => x => f(g(x));  
  
const doubleAndSquare = compose(square, double);  
  
const results = numbers.map(doubleAndSquare);  
console.log(results); // [4, 16, 36, 64, 100]  
const numbers = [1, 2, 3, 4, 5];  
  
// Functions to double and square numbers  
const double = x => x * 2;  
const square = x => x * x;  
  
// Composing functions  
const compose = (f, g) => x => f(g(x));  
  
const doubleAndSquare = compose(square, double);  
  
const results = numbers.map(doubleAndSquare);  
console.log(results); // [4, 16, 36, 64, 100]  

Leave a Reply

Your email address will not be published. Required fields are marked *

×