Facebook Pixel

reduce Advanced Examples: Grouping and Pipelining in JavaScript

reduce can do more than sum. Here are advanced examples: grouping, pipelining, and more.

reduce Advanced Examples: Grouping and Pipelining in JavaScript

reduce is the most versatile array method. Here are advanced examples beyond simple sum.

Group By Property

const people = [ { name: "A", dept: "eng" }, { name: "B", dept: "sales" }, { name: "C", dept: "eng" }, ]; const byDept = people.reduce((acc, p) => { (acc[p.dept] ||= []).push(p); return acc; }, {}); // { eng: [A, C], sales: [B] } `` ### Count Occurrences ```js const words = ["a", "b", "a", "c", "b", "a"]; const count = words.reduce((acc, w) => { acc[w] = (acc[w] || 0) + 1; return acc; }, {}); // { a: 3, b: 2, c: 1 } `` ### Build a Lookup Object ```js const users = [{ id: 1, name: "A" }, { id: 2, name: "B" }]; const byId = users.reduce((acc, u) => { acc[u.id] = u; return acc; }, {}); // { 1: { id: 1, name: "A" }, 2: { id: 2, name: "B" } } byId[1].name; // "A" (O(1) lookup) `` ### Flatten Deeply ```js const nested = [1, [2, [3, [4]]]]; const flat = nested.reduce((acc, el) => { if (Array.isArray(el)) return acc.concat(flatten(el)); return acc.concat(el); }, []); // [1, 2, 3, 4] `` Or use `flat(Infinity)`. ### Pipe (Function Composition) ```js const pipe = (...fns) => (x) => fns.reduce((acc, fn) => fn(acc), x); const pipeline = pipe( (x) => x + 1, (x) => x * 2, (x) => x - 3 ); pipeline(5); // (5 + 1) * 2 - 3 = 9 `` ### Chunk an Array ```js const arr = [1, 2, 3, 4, 5, 6, 7]; const chunks = arr.reduce((acc, n, i) => { const chunkIndex = Math.floor(i / 3); (acc[chunkIndex] ||= []).push(n); return acc; }, []); // [[1, 2, 3], [4, 5, 6], [7]] `` ### Reverse an Array (Without `reverse`) ```js [1, 2, 3].reduce((acc, n) => [n, ...acc], []); // [3, 2, 1] `` ### Find the Most Frequent ```js const arr = ["a", "b", "a", "c", "a", "b"]; const mostFrequent = arr.reduce((acc, item) => { acc[item] = (acc[item] || 0) + 1; if (acc[item] > (acc.maxCount || 0)) { acc.maxCount = acc[item]; acc.maxItem = item; } return acc; }, {}).maxItem; // "a" `` ### The Takeaway `reduce` can group by property, count occurrences, build lookup objects, flatten deeply, pipe functions, chunk arrays, reverse, and find the most frequent. It is the most versatile array method. Always pass an initial value and return the accumulator.

Use an object as the initial value. For each element, push it to the array at acc[property]: people.reduce((acc, p) => { (acc[p.dept] ||= []).push(p); return acc; }, {}).

Use an object as the initial value. For each element, increment the count: arr.reduce((acc, w) => { acc[w] = (acc[w] || 0) + 1; return acc; }, {}).

Use an object as the initial value. For each element, set acc[element.id] = element: users.reduce((acc, u) => { acc[u.id] = u; return acc; }, {}). This gives O(1) lookup by id.

Use an array of functions as the initial value: fns.reduce((acc, fn) => fn(acc), x). This applies each function in sequence to the accumulator. This is the basis of the pipe pattern.

Use an array as the initial value. Calculate the chunk index: Math.floor(i / size). Push the element to acc[chunkIndex]: arr.reduce((acc, n, i) => { (acc[Math.floor(i / 3)] ||= []).push(n); return acc; }, []).

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.