Skip to main content
TOPIC 6 OF 7 · 3 MIN READ

Applying Higher-Order Functions (HOFs) for Searching, Filtering, Pagination, and Sorting

Higher-Order Functions (HOFs) operate on arrays, taking a function as an argument or returning a function. They are essential for concise, expressive, and reusable code.

Key concepts

map

Transforms each element of an array into a new array.

forEach

Iterates over an array, typically used for side effects.

filter

Creates a new array with elements that satisfy a condition.

reduce

Reduces an array to a single value by repeatedly applying a function.

sort

Reorders elements of an array based on a comparator function.

Step by step

Advantages of Using HOFs:

  • Concise Code: HOFs reduce boilerplate and improve readability.
  • Reusability: HOF-based logic can be easily reused and extended.
  • Functional Approach: Encourages immutability and avoids side effects.

Examples

Searching Logic:

const products = [
  { id: 1, name: 'Laptop', description: 'Portable computer' },
  { id: 2, name: 'Phone', description: 'Handheld device' },
  { id: 3, name: 'Tablet', description: 'Touchscreen device' }
];

// Search by keyword
const searchKeyword = 'laptop';

const searchResults = products.filter(product =>
  product.name.toLowerCase().includes(searchKeyword.toLowerCase()) ||
  product.description.toLowerCase().includes(searchKeyword.toLowerCase())
);

console.log(searchResults);
// Output: [{ id: 1, name: 'Laptop', description: 'Portable computer' }]

Filtering Logic:

const products = [
  { id: 1, name: 'Laptop', price: 1000 },
  { id: 2, name: 'Phone', price: 500 },
  { id: 3, name: 'Tablet', price: 700 }
];

// Filter products by price range
const minPrice = 600;
const maxPrice = 1200;

const filteredProducts = products.filter(product =>
  product.price >= minPrice && product.price <= maxPrice
);

console.log(filteredProducts);
// Output: [{ id: 1, name: 'Laptop', price: 1000 }, { id: 3, name: 'Tablet', price: 700 }]

Sorting Logic:

const products = [
  { id: 1, name: 'Laptop', price: 1000 },
  { id: 2, name: 'Phone', price: 500 },
  { id: 3, name: 'Tablet', price: 700 }
];

// Ascending order
const sortedAsc = products.slice().sort((a, b) => a.price - b.price);
console.log(sortedAsc);
// Output: [{ id: 2, name: 'Phone', price: 500 }, { id: 3, name: 'Tablet', price: 700 }, { id: 1, name: 'Laptop', price: 1000 }]

// Descending order
const sortedDesc = products.slice().sort((a, b) => b.price - a.price);
console.log(sortedDesc);
// Output: [{ id: 1, name: 'Laptop', price: 1000 }, { id: 3, name: 'Tablet', price: 700 }, { id: 2, name: 'Phone', price: 500 }]

Pagination Logic:

const items = Array.from({ length: 50 }, (_, i) => `Item ${i + 1}`); // Example dataset

// Pagination logic
function paginate(array, pageSize, currentPage) {
  const start = (currentPage - 1) * pageSize;
  return array.slice(start, start + pageSize);
}

// Paginate items (10 items per page, page 2)
const pageSize = 10;
const currentPage = 2;

const paginatedItems = paginate(items, pageSize, currentPage);
console.log(paginatedItems);
// Output: ["Item 11", "Item 12", ..., "Item 20"]

Combining Searching, Filtering, and Sorting:

const products = [
  { id: 1, name: 'Laptop', price: 1000 },
  { id: 2, name: 'Phone', price: 500 },
  { id: 3, name: 'Tablet', price: 700 },
  { id: 4, name: 'Smartwatch', price: 200 }
];

const searchKeyword = 'p';
const minPrice = 300;
const maxPrice = 1200;

// Combined operations
const result = products
  .filter(product =>
    product.name.toLowerCase().includes(searchKeyword.toLowerCase())
  )
  .filter(product => product.price >= minPrice && product.price <= maxPrice)
  .sort((a, b) => a.name.localeCompare(b.name));

console.log(result);
// Output: [
//   { id: 1, name: 'Laptop', price: 1000 },
//   { id: 3, name: 'Tablet', price: 700 }
// ]

Stuck on this one? Talk to Sumit.

Keys: F focus mode · ← → previous / next topic · Esc leave focus