Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 2 of 10 · Array Methods
Interview question

How does the reduce() method work and what can it be used for? reduce() method कैसे काम करता है और इसे किस लिए इस्तेमाल किया जा सकता है?

Answer

reduce() executes a reducer function on each element, accumulating a single result. It's the most powerful and flexible array method - map, filter, and even forEach can all be implemented using it.

// Basic syntax: array.reduce((accumulator, currentValue) => {...}, initialValue)
const numbers = [1, 2, 3, 4, 5];

// Sum all numbers
const sum = numbers.reduce((acc, curr) => acc + curr, 0);
console.log(sum); // 15

// Find the maximum value
const max = numbers.reduce((acc, curr) => (curr > acc ? curr : acc), numbers[0]);
console.log(max); // 5

// Count occurrences - building an object
const fruits = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple'];
const count = fruits.reduce((acc, fruit) => {
    acc[fruit] = (acc[fruit] || 0) + 1;
    return acc;
}, {});
console.log(count); // { apple: 3, banana: 2, orange: 1 }

// Grouping objects by a property
const people = [
    { name: 'John', dept: 'IT' },
    { name: 'Jane', dept: 'HR' },
    { name: 'Bob', dept: 'IT' }
];
const grouped = people.reduce((acc, person) => {
    (acc[person.dept] = acc[person.dept] || []).push(person);
    return acc;
}, {});
console.log(grouped); // { IT: [John, Bob], HR: [Jane] }

// Flattening a nested array
const nested = [[1, 2], [3, 4], [5]];
const flat = nested.reduce((acc, arr) => acc.concat(arr), []);
console.log(flat); // [1, 2, 3, 4, 5]

// Implementing map using reduce
const mapped = numbers.reduce((acc, n) => {
    acc.push(n * 2);
    return acc;
}, []);
console.log(mapped); // [2, 4, 6, 8, 10]

// Without an initial value - first element becomes the initial accumulator
const sumNoInit = numbers.reduce((acc, curr) => acc + curr);
console.log(sumNoInit); // 15 - works, but throws on empty arrays without initial value

reduce() हर element पर reducer function चलाता है, एक single result में accumulate करता है। यह सबसे powerful array method है - map, filter, forEach भी इससे implement हो सकते हैं।

const numbers = [1, 2, 3, 4, 5];

// सभी numbers जोड़ना
const sum = numbers.reduce((acc, curr) => acc + curr, 0);
console.log(sum); // 15

// Maximum value ढूंढना
const max = numbers.reduce((acc, curr) => (curr > acc ? curr : acc), numbers[0]);
console.log(max); // 5

// Occurrences count करना
const fruits = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple'];
const count = fruits.reduce((acc, fruit) => {
    acc[fruit] = (acc[fruit] || 0) + 1;
    return acc;
}, {});
console.log(count); // { apple: 3, banana: 2, orange: 1 }

// Property से group करना
const people = [
    { name: 'John', dept: 'IT' },
    { name: 'Jane', dept: 'HR' },
    { name: 'Bob', dept: 'IT' }
];
const grouped = people.reduce((acc, person) => {
    (acc[person.dept] = acc[person.dept] || []).push(person);
    return acc;
}, {});
console.log(grouped); // { IT: [...], HR: [...] }

// Nested array flatten करना
const nested = [[1, 2], [3, 4], [5]];
const flat = nested.reduce((acc, arr) => acc.concat(arr), []);
console.log(flat); // [1, 2, 3, 4, 5]

Was this answer clear?