<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Map, Filter, Reduce & More]]></title><description><![CDATA[Map, Filter, Reduce & More]]></description><link>https://jyotiraditya.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sat, 28 Nov 2026 21:04:29 GMT</lastBuildDate><atom:link href="https://jyotiraditya.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[JavaScript Array Magic: Crack .map(), .filter(), and .reduce() with Examples]]></title><description><![CDATA[Master JavaScript's powerful array methods: map(), filter(), and reduce(). Clear examples and simple explanations to level up your coding immediately.]]></description><link>https://jyotiraditya.hashnode.dev/javascript-array-magic-crack-map-filter-and-reduce-with-examples</link><guid isPermaLink="true">https://jyotiraditya.hashnode.dev/javascript-array-magic-crack-map-filter-and-reduce-with-examples</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Beginner Developers]]></category><dc:creator><![CDATA[Jyotiraditya]]></dc:creator><pubDate>Fri, 28 Feb 2025 12:36:03 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1740687468215/836b5d6b-e29c-4b84-8062-aa7c3f841987.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Every JavaScript developer comes across these powerful higher-order functions: <strong><mark>.map()</mark></strong>, <strong>.<mark>filter()</mark></strong><mark>,</mark> and <strong>.<mark>reduce()</mark></strong>. But before diving into how they work, let’s step back and understand <strong>what each one actually does</strong>. By the end of this article, you’ll not only know how to use them confidently but also have a handy reference you can come back to anytime</p>
<p>And don’t worry if you’re not fully sure what higher-order functions are — as we go through the examples, you’ll naturally get a sense of it.</p>
<h2 id="heading-higher-order-functions">Higher Order Functions</h2>
<p>In simple terms, <strong>higher-order functions</strong> are just functions that <strong>take another function as an argument</strong>.</p>
<p>(If you’re coming from a different programming language, this might sound like — <em>wait, what the hell?</em> — but stick with me!)</p>
<p>For now, don’t worry about the deep mechanics. Just remember:<br />✅ A <strong>function taking another function</strong> — that’s all a higher-order function is.</p>
<p>Here’s a quick example to make it clearer:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">greetUser</span>(<span class="hljs-params">name, anotherFunction</span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hello "</span> + name);
    anotherFunction(); <span class="hljs-comment">// Calling the function passed as argument</span>
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">showWelcomeMessage</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Welcome to the blog!"</span>);
}

<span class="hljs-comment">// Calling the higher-order function</span>
greetUser(<span class="hljs-string">"Jyotir"</span>, showWelcomeMessage);

<span class="hljs-comment">//OUTPUT</span>
<span class="hljs-comment">/* Hello Jyotir
Welcome to the blog! */</span>
</code></pre>
<p>See that? <code>greetUser</code> takes <code>showWelcomeMessage</code> as a <strong>parameter</strong> — boom, that’s a higher-order function!</p>
<h2 id="heading-map"><mark>.Map()</mark> 🤯</h2>
<p>Imagine this — you have an array of numbers, and you want to <strong>double</strong> each number. If you didn’t know about <code>.map()</code>, you would probably write something like this:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> arr = [<span class="hljs-number">2</span>,<span class="hljs-number">4</span>,<span class="hljs-number">3</span>,<span class="hljs-number">6</span>,<span class="hljs-number">10</span>];
<span class="hljs-keyword">let</span> output = [];
<span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; arr.length; i++){
    output.push(arr[i]*<span class="hljs-number">2</span>);
}
<span class="hljs-built_in">console</span>.log(output); <span class="hljs-comment">// [4, 8, 6, 12, 20]</span>
</code></pre>
<p>Now this is fine ,but remember the higher-order function we saw earlier? Well, <code>.map()</code> makes your life <strong>way easier</strong> when you want to <strong>transform</strong> each item in an array.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> arr  = [<span class="hljs-number">2</span>,<span class="hljs-number">4</span>,<span class="hljs-number">3</span>,<span class="hljs-number">6</span>,<span class="hljs-number">10</span>];
<span class="hljs-keyword">let</span> output = arr.map(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">item</span>)=&gt; </span>{
    <span class="hljs-keyword">return</span> item * <span class="hljs-number">2</span>;
})
<span class="hljs-built_in">console</span>.log(output); <span class="hljs-comment">// [4, 8, 6, 12, 20]</span>
</code></pre>
<h3 id="heading-think-of-map-like-this">Think of <code>.map()</code> like this:</h3>
<ol>
<li><p>You give <code>.map()</code> a list of items (the array).</p>
</li>
<li><p>You also give it a <strong>machine (the function)</strong> that knows what to do with each item.</p>
</li>
<li><p><code>.map()</code> runs all the items through the machine and gives you a <strong>new array</strong> with transformed items.</p>
</li>
</ol>
<h3 id="heading-what-just-happened">What just happened?</h3>
<p>Let’s break it down (this will <strong>stick in your head forever</strong> — promise):</p>
<p>✅ <code>.map()</code> is a <strong>higher-order function</strong> because it takes a <strong>function as a parameter</strong>.<br />✅ This function (often called a <strong>callback function</strong>) runs once for <strong>each item</strong> in the array.<br />✅ Inside this function, you get <strong>each item</strong> one-by-one (that’s the <code>item</code>).<br />✅ Whatever this function <strong>returns</strong>, <code>.map()</code> <strong>collects into a new array</strong>.<br />✅ So, for <code>arr[0] = 2</code>, the function runs and returns <code>2 * 2 = 4</code>. Same for all other items.</p>
<h3 id="heading-when-should-we-use-map-with-examples">When should we use <code>.map()</code>? (with examples)</h3>
<p>Use <code>.map()</code> whenever you want to <strong>transform each item in an array</strong> — meaning you want to perform <strong>some operation on every item</strong> and <strong>return a new array</strong> with the transformed items.</p>
<p>Here are some examples to make this super clear:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Example 1: Double every number</span>
<span class="hljs-keyword">let</span> numbers = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>];
<span class="hljs-keyword">let</span> doubled = numbers.map(<span class="hljs-function"><span class="hljs-params">num</span> =&gt;</span> num * <span class="hljs-number">2</span>); <span class="hljs-comment">// shorter syntax no need of writing return </span>
<span class="hljs-built_in">console</span>.log(doubled); <span class="hljs-comment">// [2, 4, 6, 8, 10]</span>

<span class="hljs-comment">// Example 2: Add "Mr." to every name</span>
<span class="hljs-keyword">let</span> names = [<span class="hljs-string">"John"</span>, <span class="hljs-string">"Doe"</span>, <span class="hljs-string">"Jane"</span>];
<span class="hljs-keyword">let</span> updatedNames = names.map(<span class="hljs-function"><span class="hljs-params">name</span> =&gt;</span> <span class="hljs-string">"Mr. "</span> + name);
<span class="hljs-built_in">console</span>.log(updatedNames); <span class="hljs-comment">// ["Mr. John", "Mr. Doe", "Mr. Jane"]</span>

<span class="hljs-comment">// Example 3: Extract specific property from objects</span>
<span class="hljs-keyword">let</span> users = [
    { <span class="hljs-attr">name</span>: <span class="hljs-string">"Alice"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">25</span> },
    { <span class="hljs-attr">name</span>: <span class="hljs-string">"Bob"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">30</span> },
    { <span class="hljs-attr">name</span>: <span class="hljs-string">"Charlie"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">35</span> }
];
<span class="hljs-keyword">let</span> namesOnly = users.map(<span class="hljs-function"><span class="hljs-params">user</span> =&gt;</span> user.name);
<span class="hljs-built_in">console</span>.log(namesOnly);<span class="hljs-comment">// ["Alice", "Bob", "Charlie"]</span>
</code></pre>
<h2 id="heading-filter-does-it-filter-stuff"><mark>.Filter()</mark> — Does it filter stuff? 😎</h2>
<p>Yes! It does exactly what the name suggests — <strong>it filters stuff</strong>!<br />Imagine you have a list of people, and you only want the ones <strong>above 40 years old</strong>.</p>
<p>Before <code>.filter()</code> existed (or before you knew about it), you might have written this:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> people = [
    { <span class="hljs-attr">name</span>: <span class="hljs-string">"Alice"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">25</span> },
    { <span class="hljs-attr">name</span>: <span class="hljs-string">"Bob"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">45</span> },
    { <span class="hljs-attr">name</span>: <span class="hljs-string">"Charlie"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">35</span> },
    { <span class="hljs-attr">name</span>: <span class="hljs-string">"David"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">50</span> }
];

<span class="hljs-keyword">let</span> aboveForty = [];

<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; people.length; i++) {
    <span class="hljs-keyword">if</span> (people[i].age &gt; <span class="hljs-number">40</span>) {
        aboveForty.push(people[i]);
    }
}

<span class="hljs-built_in">console</span>.log(aboveForty); <span class="hljs-comment">// [{ name: "Bob", age: 45 }, { name: "David", age: 50 }]</span>
</code></pre>
<p>But now, with the <strong>help of</strong> <code>.filter()</code>, this becomes much <strong>cleaner</strong> and way more <strong>readable</strong>:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> people = [
    { <span class="hljs-attr">name</span>: <span class="hljs-string">"Alice"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">25</span> },
    { <span class="hljs-attr">name</span>: <span class="hljs-string">"Bob"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">45</span> },
    { <span class="hljs-attr">name</span>: <span class="hljs-string">"Charlie"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">35</span> },
    { <span class="hljs-attr">name</span>: <span class="hljs-string">"David"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">50</span> }
];


<span class="hljs-keyword">let</span> aboveForty = people.filter(<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">logic</span>(<span class="hljs-params">person</span>) </span>{
    <span class="hljs-keyword">if</span> (person.age &gt; <span class="hljs-number">40</span>) {
        <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>; <span class="hljs-comment">// Only those who pass this test stay in the new array</span>
    }
});

<span class="hljs-built_in">console</span>.log(aboveForty); <span class="hljs-comment">//[{ name: "Bob", age: 45 },{ name: "David", age: 50 }]</span>
</code></pre>
<h3 id="heading-no-manual-looping">✅ No manual looping</h3>
<p>✅ No need to handle your own <code>push</code><br />✅ Just write the <strong>logic</strong>, and <code>.filter()</code> takes care of the rest!  </p>
<p>🔍 What just happened? What kind of magic is this?</p>
<p>The <strong>logic function inside</strong> <code>.filter()</code> runs on <strong>each item (person) of the</strong> <code>people</code> array.</p>
<ul>
<li><p>If the function returns <strong>true</strong>, that item is <strong>automatically added</strong> to <code>aboveForty</code>.</p>
</li>
<li><p>If it returns <strong>false</strong>, that item is <strong>ignored</strong>.</p>
</li>
</ul>
<p>No need to manually loop, check conditions, or push items into a new array—<code>.filter()</code> does it all for you!</p>
<h3 id="heading-shorter-version-using-an-arrow-function">🎯 Shorter version using an arrow function</h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> people = [
    { <span class="hljs-attr">name</span>: <span class="hljs-string">"Alice"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">25</span> },
    { <span class="hljs-attr">name</span>: <span class="hljs-string">"Bob"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">45</span> },
    { <span class="hljs-attr">name</span>: <span class="hljs-string">"Charlie"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">35</span> },
    { <span class="hljs-attr">name</span>: <span class="hljs-string">"David"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">50</span> }
];

<span class="hljs-keyword">let</span> aboveForty = people.filter(<span class="hljs-function"><span class="hljs-params">person</span> =&gt;</span> person.age &gt; <span class="hljs-number">40</span>);
<span class="hljs-built_in">console</span>.log(aboveForty);
</code></pre>
<h3 id="heading-when-should-we-use-filter-with-examples">When should we use <code>.filter()</code>? (with examples)</h3>
<p>Whenever you want to <strong>pick only certain items</strong> from an array based on some condition — <code>.filter()</code> is your go-to hero! It <strong>returns a new array</strong> with only the items that passed the test (items where your function returned <code>true</code>).</p>
<p>Here are some super-clear examples to lock this into your brain:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Example 1: Find numbers greater than 50</span>
<span class="hljs-keyword">let</span> numbers = [<span class="hljs-number">10</span>, <span class="hljs-number">55</span>, <span class="hljs-number">23</span>, <span class="hljs-number">78</span>, <span class="hljs-number">92</span>, <span class="hljs-number">15</span>];
<span class="hljs-keyword">let</span> greaterThanFifty = numbers.filter(<span class="hljs-function"><span class="hljs-params">num</span> =&gt;</span> num &gt; <span class="hljs-number">50</span>);
<span class="hljs-built_in">console</span>.log(greaterThanFifty); <span class="hljs-comment">// [55, 78, 92]</span>

<span class="hljs-comment">// Example 2: Filter completed tasks from a to-do list</span>
<span class="hljs-keyword">let</span> tasks = [
    { <span class="hljs-attr">title</span>: <span class="hljs-string">"Learn .map()"</span>, <span class="hljs-attr">completed</span>: <span class="hljs-literal">true</span> },
    { <span class="hljs-attr">title</span>: <span class="hljs-string">"Learn .filter()"</span>, <span class="hljs-attr">completed</span>: <span class="hljs-literal">false</span> },
    { <span class="hljs-attr">title</span>: <span class="hljs-string">"Write blog"</span>, <span class="hljs-attr">completed</span>: <span class="hljs-literal">true</span> }
];
<span class="hljs-keyword">let</span> completedTasks = tasks.filter(<span class="hljs-function"><span class="hljs-params">task</span> =&gt;</span> task.completed);
<span class="hljs-built_in">console</span>.log(completedTasks);
</code></pre>
<h2 id="heading-reduce-does-it-reduce-the-array"><mark>.Reduce()</mark> - Does it reduce the array ? 😥</h2>
<p>Kinda of yes , lets learn this by example, theory can only take you up to a level</p>
<h3 id="heading-what-problem-does-reduce-solve">What problem does <code>.reduce()</code> solve?</h3>
<p>Imagine you have a list of numbers, and you want to <strong>find the total sum</strong>. Without <code>.reduce()</code>, you’d probably do something like this:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> numbers = [<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>, <span class="hljs-number">40</span>];
<span class="hljs-keyword">let</span> sum = <span class="hljs-number">0</span>;

<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; numbers.length; i++) {
    sum += numbers[i];
}

<span class="hljs-built_in">console</span>.log(sum); <span class="hljs-comment">// 100</span>
</code></pre>
<h3 id="heading-enter-reduce">Enter <code>.reduce()</code></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> numbers = [<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>, <span class="hljs-number">40</span>];

<span class="hljs-keyword">let</span> sum = numbers.reduce(<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sumAll</span>(<span class="hljs-params">accumulator, currentElement</span>) </span>{
    accumulator =  accumulator + currentElement;
    <span class="hljs-keyword">return</span> accumulator;
}, <span class="hljs-number">0</span>);

<span class="hljs-built_in">console</span>.log(sum); <span class="hljs-comment">// 100</span>
</code></pre>
<p>Now, I know <code>.reduce()</code> feels a bit more <strong>complicated</strong> than <code>.map()</code> and <code>.filter()</code>, so let’s break it down piece by piece — <strong>no rush, we’ll get this sorted</strong>.</p>
<h3 id="heading-what-is-an-accumulator">What is an <strong>accumulator</strong>? 🤔</h3>
<p>The <strong>accumulator</strong> is exactly what the name says — it <strong>collects and builds up a value</strong> as <code>.reduce()</code> loops through the array. Think of it like the <code>sum</code> variable we used in the old-school <code>for</code> loop. Each time the loop runs, the accumulator grows.</p>
<h3 id="heading-what-is-currentelement">What is <code>currentElement</code>?</h3>
<p>This one’s easy — it’s just the <strong>current item in the array</strong>, exactly like in <code>.map()</code> and <code>.filter()</code>.</p>
<h3 id="heading-whats-that-second-parameter-the-0">What’s that second parameter (the <code>0</code>)?</h3>
<p>That’s the <strong>initial value of the accumulator</strong>. Think of it like when you do:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> sum = <span class="hljs-number">0</span>;
</code></pre>
<p>This <code>0</code> is the starting point. If you were finding the <strong>product</strong> of all numbers, you might start with <code>1</code>. For a string concatenation, you could start with an <strong>empty string</strong> (<code>""</code>).</p>
<p>Here’s that full pattern again:</p>
<pre><code class="lang-javascript">array.reduce(callbackFunction, initialValue)
</code></pre>
<p>The <strong>callbackFunction</strong> gets 2 main arguments:</p>
<ol>
<li><p><code>accumulator</code> — the running total (or combined value so far)</p>
</li>
<li><p><code>currentElement</code> — the current item from the array</p>
</li>
</ol>
<h3 id="heading-bringing-it-all-together">Bringing it all together</h3>
<p>Let’s rewrite the earlier example with better clarity:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> numbers = [<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>, <span class="hljs-number">40</span>];

<span class="hljs-keyword">let</span> sum = numbers.reduce(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">accumulator, currentElement</span>) </span>{
    <span class="hljs-keyword">return</span> accumulator + currentElement;
}, <span class="hljs-number">0</span>);

<span class="hljs-built_in">console</span>.log(sum); <span class="hljs-comment">// 100</span>
</code></pre>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Step</td><td>Current Number (<code>num</code>)</td><td>Accumulator (<code>acc</code>)</td></tr>
</thead>
<tbody>
<tr>
<td>Initial</td><td>-</td><td>0</td></tr>
<tr>
<td>1</td><td>10</td><td>0 + 10 = 10</td></tr>
<tr>
<td>2</td><td>20</td><td>10 + 20 = 30</td></tr>
<tr>
<td>3</td><td>30</td><td>30 + 30 = 60</td></tr>
<tr>
<td>4</td><td>40</td><td>60 + 40 = 100</td></tr>
</tbody>
</table>
</div><ol>
<li><p>This is almost the same as:</p>
<pre><code class="lang-javascript"> <span class="hljs-keyword">let</span> sum = <span class="hljs-number">0</span>;
 <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; numbers.length; i++) {
     sum = sum + numbers[i];
 }
</code></pre>
</li>
</ol>
<p>    Key takeaway 🚀</p>
<p>    ✅ <code>.reduce()</code> can do <strong>way more than just sums</strong> — you can use it to build arrays, objects, or even filter things if you want to get creative!<br />    ✅ That initial value is <strong>crucial</strong> — it sets the tone for your accumulator.<br />    ✅ If you master <code>.reduce()</code>, you unlock a whole new level of <strong>array wizardry</strong>.</p>
<h2 id="heading-when-should-we-use-reduce-with-examples">When should we use <code>.reduce()</code>? (with examples)</h2>
<h3 id="heading-use-reduce-when-you-want-to-boil-down-an-array-into-a-single-value">Use <code>.reduce()</code> when you want to <strong>boil down an array into a single value</strong>.</h3>
<p>This "single value" could be:</p>
<p>✅ A <strong>sum</strong> (like we saw before)<br />✅ A <strong>product</strong> (multiplying everything together)<br />✅ A <strong>single object</strong> (combining data into one object)<br />✅ Even a <strong>new array</strong> (yup, <code>.reduce()</code> can do that too!)</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Example 1: Sum all numbers</span>
<span class="hljs-keyword">let</span> numbers = [<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>, <span class="hljs-number">40</span>];
<span class="hljs-keyword">let</span> sum = numbers.reduce(<span class="hljs-function">(<span class="hljs-params">acc, num</span>) =&gt;</span> acc + num, <span class="hljs-number">0</span>);
<span class="hljs-built_in">console</span>.log(sum); <span class="hljs-comment">// 100</span>

<span class="hljs-comment">// Example 2: Find the maximum number</span>

<span class="hljs-keyword">let</span> numbers = [<span class="hljs-number">5</span>, <span class="hljs-number">8</span>, <span class="hljs-number">2</span>, <span class="hljs-number">9</span>, <span class="hljs-number">3</span>];
<span class="hljs-keyword">let</span> max = numbers.reduce(<span class="hljs-function">(<span class="hljs-params">acc, num</span>) =&gt;</span> {
    <span class="hljs-keyword">return</span> num &gt; acc ? num : acc;
}, numbers[<span class="hljs-number">0</span>]); <span class="hljs-comment">// Start with first element as initial value</span>

<span class="hljs-built_in">console</span>.log(max); <span class="hljs-comment">// 9</span>

<span class="hljs-comment">// Example 3: Group items by property (a little advance!)</span>
<span class="hljs-keyword">let</span> people = [
    { <span class="hljs-attr">name</span>: <span class="hljs-string">"Alice"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">25</span> },
    { <span class="hljs-attr">name</span>: <span class="hljs-string">"Bob"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">45</span> },
    { <span class="hljs-attr">name</span>: <span class="hljs-string">"Charlie"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">35</span> },
    { <span class="hljs-attr">name</span>: <span class="hljs-string">"David"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">25</span> }
];

<span class="hljs-keyword">let</span> groupedByAge = people.reduce(<span class="hljs-function">(<span class="hljs-params">acc, person</span>) =&gt;</span> {
    <span class="hljs-keyword">if</span> (!acc[person.age]) {
        acc[person.age] = [];
    }
    acc[person.age].push(person.name);
    <span class="hljs-keyword">return</span> acc;
}, {});

<span class="hljs-built_in">console</span>.log(groupedByAge);
<span class="hljs-comment">// Output:</span>
<span class="hljs-comment">// {</span>
<span class="hljs-comment">//   25: ["Alice", "David"],</span>
<span class="hljs-comment">//   45: ["Bob"],</span>
<span class="hljs-comment">//   35: ["Charlie"]</span>
<span class="hljs-comment">// }</span>
</code></pre>
<p>One-Liner Summary 📚</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Use This</td><td>When You Want To</td></tr>
</thead>
<tbody>
<tr>
<td><code>.map()</code></td><td>Transform each item into something new</td></tr>
<tr>
<td><code>.filter()</code></td><td>Pick some items (based on a condition)</td></tr>
<tr>
<td><code>.reduce()</code></td><td>Combine everything into <strong>one final result</strong></td></tr>
</tbody>
</table>
</div><h2 id="heading-chaining-higher-order-functions-map-filter-reduce">Chaining Higher-Order Functions (<code>.map()</code>, <code>.filter()</code>, <code>.reduce()</code>)</h2>
<p>Chaining means you <strong>call multiple methods one after the other</strong> on the same array. Each method passes its <strong>result</strong> to the next method in the chain.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> numbers = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>, <span class="hljs-number">10</span>];

<span class="hljs-keyword">let</span> result = numbers
    .filter(<span class="hljs-function"><span class="hljs-params">num</span> =&gt;</span> num % <span class="hljs-number">2</span> === <span class="hljs-number">0</span>)        <span class="hljs-comment">// Step 1: Keep only even numbers</span>
    .map(<span class="hljs-function"><span class="hljs-params">num</span> =&gt;</span> num * <span class="hljs-number">2</span>)                  <span class="hljs-comment">// Step 2: Double each even number</span>
    .reduce(<span class="hljs-function">(<span class="hljs-params">sum, num</span>) =&gt;</span> sum + num, <span class="hljs-number">0</span>);  <span class="hljs-comment">// Step 3: Sum them all up</span>

<span class="hljs-built_in">console</span>.log(result);  <span class="hljs-comment">// Output: 60</span>
</code></pre>
<p>Some examples are given below</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Example 1 — Extracting Names of Active Users</span>

<span class="hljs-keyword">let</span> users = [
    { <span class="hljs-attr">name</span>: <span class="hljs-string">"Alice"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">25</span>, <span class="hljs-attr">isActive</span>: <span class="hljs-literal">true</span> },
    { <span class="hljs-attr">name</span>: <span class="hljs-string">"Bob"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">45</span>, <span class="hljs-attr">isActive</span>: <span class="hljs-literal">false</span> },
    { <span class="hljs-attr">name</span>: <span class="hljs-string">"Charlie"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">35</span>, <span class="hljs-attr">isActive</span>: <span class="hljs-literal">true</span> },
    { <span class="hljs-attr">name</span>: <span class="hljs-string">"David"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">50</span>, <span class="hljs-attr">isActive</span>: <span class="hljs-literal">false</span> }
];

<span class="hljs-keyword">let</span> activeUserNames = users
    .filter(<span class="hljs-function"><span class="hljs-params">user</span> =&gt;</span> user.isActive)     <span class="hljs-comment">// Keep only active users</span>
    .map(<span class="hljs-function"><span class="hljs-params">user</span> =&gt;</span> user.name);            <span class="hljs-comment">// Extract their names</span>

<span class="hljs-built_in">console</span>.log(activeUserNames);  <span class="hljs-comment">// ["Alice", "Charlie"]</span>


<span class="hljs-comment">// Example 2 — Find Average Age of Students who Passed</span>
<span class="hljs-keyword">let</span> students = [
    { <span class="hljs-attr">name</span>: <span class="hljs-string">"John"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">18</span>, <span class="hljs-attr">score</span>: <span class="hljs-number">85</span> },
    { <span class="hljs-attr">name</span>: <span class="hljs-string">"Sarah"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">19</span>, <span class="hljs-attr">score</span>: <span class="hljs-number">45</span> },
    { <span class="hljs-attr">name</span>: <span class="hljs-string">"Mike"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">20</span>, <span class="hljs-attr">score</span>: <span class="hljs-number">70</span> },
    { <span class="hljs-attr">name</span>: <span class="hljs-string">"Emma"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">21</span>, <span class="hljs-attr">score</span>: <span class="hljs-number">90</span> }
];

<span class="hljs-keyword">let</span> averageAge = students
    .filter(<span class="hljs-function"><span class="hljs-params">student</span> =&gt;</span> student.score &gt;= <span class="hljs-number">50</span>)  <span class="hljs-comment">// Only those who passed</span>
    .map(<span class="hljs-function"><span class="hljs-params">student</span> =&gt;</span> student.age)              <span class="hljs-comment">// Extract ages</span>
    .reduce(<span class="hljs-function">(<span class="hljs-params">sum, age, _, arr</span>) =&gt;</span> sum + age / arr.length, <span class="hljs-number">0</span>);  <span class="hljs-comment">// Average age calculation</span>

<span class="hljs-built_in">console</span>.log(averageAge);  <span class="hljs-comment">// Average age of passed students</span>
</code></pre>
<h1 id="heading-additional-resources-level-up-your-learning">Additional Resources — Level Up Your Learning</h1>
<h3 id="heading-official-documentation">Official Documentation</h3>
<ul>
<li><p><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map">MDN Documentation for Array.map()</a></p>
</li>
<li><p><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter">MDN Documentation for Array.filter()</a></p>
</li>
<li><p><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce">MDN Documentation for Array.reduce()</a></p>
</li>
</ul>
<h3 id="heading-recommended-youtube-tutorials">Recommended YouTube Tutorials</h3>
<ul>
<li><p>🎥 <a target="_blank" href="https://youtu.be/zdp0zrpKzIE?si=9Hp4NmTjDMJO140J">Namaste JavaScript 🙏 Course - JS Video Tutorials by Akshay Saini</a></p>
</li>
<li><p>🎥 <a target="_blank" href="https://youtu.be/HkWxvB1RJq0?si=rgHs8lP85YARa7Hr">Higher Order Functions - Akshay Saini</a></p>
</li>
</ul>
]]></content:encoded></item></channel></rss>