DEV Community

Cover image for Practicing JavaScript Data Structures Without map/filter/reduce
Amir
Amir

Posted on

Practicing JavaScript Data Structures Without map/filter/reduce


_I’ve been practicing JavaScript data structures with a strict rule set:
_ • no DOM
• no map, filter, or reduce
• only objects, arrays, loops, and conditionals

The focus wasn’t syntax — it was learning how to think in data flow.

Exercise 1: Daily Store Report

From a flat list of products, generate:
• items that must be reordered
• items that are risky based on time in store

Rules:
• negative stock → 0
• missing values → 0
• reorder if stock < min
• risky if daysInStore >= 5

function buildDayliyReports(products) {
const orderList = [];
const riskyProducts = [];

for (const item of products) {
const { name, stock, min, daysInStore } = item;

const minValue = min ?? 0;
const days = daysInStore ?? 0;
let currentStock = stock ?? 0;
if (currentStock < 0) currentStock = 0;

if (currentStock < minValue) {
  const order = minValue - currentStock;
  orderList.push({ name, order });
}

if (days >= 5) {
  riskyProducts.push(name);
}
Enter fullscreen mode Exit fullscreen mode

}

return { orderList, riskyProducts };
}

Exercise 2: Supplier Report (Dynamic Grouping)

Extend the same logic to:
• group orders by category
• compute totals per category
• handle missing categories safely

function buildSuppliersReport(products) {
const orderByCategory = { uncategorised: [] };
const riskyProducts = [];
const totals = { uncategorised: 0 };

for (const item of products) {
const { name, category, stock, min, daysInStore } = item;

const minValue = min ?? 0;
const days = daysInStore ?? 0;
let currentStock = stock ?? 0;
if (currentStock < 0) currentStock = 0;

const cat = category ?? "uncategorised";

if (!(cat in orderByCategory)) orderByCategory[cat] = [];
if (!(cat in totals)) totals[cat] = 0;

if (currentStock < minValue) {
  const order = minValue - currentStock;
  orderByCategory[cat].push({ name, order });
  totals[cat] += order;
}

if (days >= 5) {
  riskyProducts.push(name);
}
Enter fullscreen mode Exit fullscreen mode

}

return { orderByCategory, riskyProducts, totals };
}

Takeaway

What helped me most was following this order consistently:
1. Normalize data
2. Make decisions independently
3. Aggregate results explicitly

This approach made nested data feel manageable and prepared me for more complex state logic.

Top comments (0)