<?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[dom, js]]></title><description><![CDATA[dom, js]]></description><link>https://domjs.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sat, 24 Oct 2026 14:44:23 GMT</lastBuildDate><atom:link href="https://domjs.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Learning Implementation]]></title><description><![CDATA[✅ Best For: Attaching event listeners to multiple elements Iterating over NodeLists (e.g., .querySelectorAll()) Showing/hiding items Adding/removing…]]></description><link>https://domjs.hashnode.dev/learning-implementation</link><guid isPermaLink="true">https://domjs.hashnode.dev/learning-implementation</guid><dc:creator><![CDATA[Maurya kavi]]></dc:creator><pubDate>Sun, 06 Apr 2025 07:26:58 GMT</pubDate><content:encoded><![CDATA[<h2 id="heading-real-world-dom-use-cases-with-array-methods">🧱 Real-World DOM Use-Cases with Array Methods</h2>
<hr />
<h3 id="heading-foreach-in-the-dom">🔁 <code>forEach()</code> in the DOM</h3>
<p>✅ <strong>Best For:</strong></p>
<ul>
<li><p>Attaching event listeners to multiple elements</p>
</li>
<li><p>Iterating over NodeLists (e.g., <code>.querySelectorAll()</code>)</p>
</li>
<li><p>Showing/hiding items</p>
</li>
<li><p>Adding/removing classes</p>
</li>
</ul>
<pre><code class="lang-javascript">javascriptCopyEditconst buttons = <span class="hljs-built_in">document</span>.querySelectorAll(<span class="hljs-string">".accordion-button"</span>);

buttons.forEach(<span class="hljs-function">(<span class="hljs-params">btn, i</span>) =&gt;</span> {
  btn.addEventListener(<span class="hljs-string">"click"</span>, <span class="hljs-function">() =&gt;</span> {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Button <span class="hljs-subst">${i}</span> clicked`</span>);
    btn.classList.toggle(<span class="hljs-string">"active"</span>);
  });
});
</code></pre>
<hr />
<h3 id="heading-map-in-the-dom">🧬 <code>map()</code> in the DOM</h3>
<p>✅ <strong>Best For:</strong></p>
<ul>
<li><p>Generating HTML dynamically from data</p>
</li>
<li><p>Returning transformed elements (can be used with <code>.innerHTML</code>)</p>
</li>
</ul>
<pre><code class="lang-javascript">javascriptCopyEditconst users = [
  { <span class="hljs-attr">name</span>: <span class="hljs-string">"Alice"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">22</span> },
  { <span class="hljs-attr">name</span>: <span class="hljs-string">"Bob"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">25</span> },
];

<span class="hljs-keyword">const</span> container = <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">"#user-list"</span>);

container.innerHTML = users
  .map(<span class="hljs-function"><span class="hljs-params">user</span> =&gt;</span> <span class="hljs-string">`&lt;div class="user"&gt;<span class="hljs-subst">${user.name}</span> - <span class="hljs-subst">${user.age}</span>&lt;/div&gt;`</span>)
  .join(<span class="hljs-string">""</span>);
</code></pre>
<hr />
<h3 id="heading-filter-in-the-dom">🔍 <code>filter()</code> in the DOM</h3>
<p>✅ <strong>Best For:</strong></p>
<ul>
<li><p>Showing only certain elements (e.g., filtering search results)</p>
</li>
<li><p>Creating dynamic UIs (e.g., category filters)</p>
</li>
</ul>
<pre><code class="lang-javascript">javascriptCopyEditconst items = [
  { <span class="hljs-attr">name</span>: <span class="hljs-string">"Laptop"</span>, <span class="hljs-attr">type</span>: <span class="hljs-string">"electronics"</span> },
  { <span class="hljs-attr">name</span>: <span class="hljs-string">"T-shirt"</span>, <span class="hljs-attr">type</span>: <span class="hljs-string">"clothing"</span> },
  { <span class="hljs-attr">name</span>: <span class="hljs-string">"Phone"</span>, <span class="hljs-attr">type</span>: <span class="hljs-string">"electronics"</span> },
];

<span class="hljs-keyword">const</span> electronics = items.filter(<span class="hljs-function"><span class="hljs-params">item</span> =&gt;</span> item.type === <span class="hljs-string">"electronics"</span>);

<span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">"#items"</span>).innerHTML = electronics
  .map(<span class="hljs-function"><span class="hljs-params">item</span> =&gt;</span> <span class="hljs-string">`&lt;li&gt;<span class="hljs-subst">${item.name}</span>&lt;/li&gt;`</span>)
  .join(<span class="hljs-string">""</span>);
</code></pre>
<hr />
<h3 id="heading-reduce-in-the-dom">➕ <code>reduce()</code> in the DOM</h3>
<p>✅ <strong>Best For:</strong></p>
<ul>
<li><p>Calculating totals (e.g., shopping cart totals)</p>
</li>
<li><p>Grouping data for display</p>
</li>
</ul>
<pre><code class="lang-javascript">javascriptCopyEditconst cart = [
  { <span class="hljs-attr">name</span>: <span class="hljs-string">"Phone"</span>, <span class="hljs-attr">price</span>: <span class="hljs-number">400</span> },
  { <span class="hljs-attr">name</span>: <span class="hljs-string">"Charger"</span>, <span class="hljs-attr">price</span>: <span class="hljs-number">40</span> },
];

<span class="hljs-keyword">const</span> total = cart.reduce(<span class="hljs-function">(<span class="hljs-params">sum, item</span>) =&gt;</span> sum + item.price, <span class="hljs-number">0</span>);

<span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">"#total"</span>).textContent = <span class="hljs-string">`Total: $<span class="hljs-subst">${total}</span>`</span>;
</code></pre>
<hr />
<h3 id="heading-find-and-findindex-in-the-dom">🔍 <code>find()</code> and <code>findIndex()</code> in the DOM</h3>
<p>✅ <strong>Best For:</strong></p>
<ul>
<li><p>Selecting a single element from a list</p>
</li>
<li><p>Handling "Edit" actions for dynamic content</p>
</li>
</ul>
<pre><code class="lang-javascript">javascriptCopyEditconst users = [
  { <span class="hljs-attr">id</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">name</span>: <span class="hljs-string">"Alice"</span> },
  { <span class="hljs-attr">id</span>: <span class="hljs-number">2</span>, <span class="hljs-attr">name</span>: <span class="hljs-string">"Bob"</span> },
];

<span class="hljs-keyword">const</span> userToEdit = users.find(<span class="hljs-function"><span class="hljs-params">user</span> =&gt;</span> user.id === <span class="hljs-number">2</span>);

<span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">"#edit-name"</span>).value = userToEdit.name;
</code></pre>
<hr />
<h3 id="heading-some-and-every-in-the-dom">✅ <code>some()</code> and <code>every()</code> in the DOM</h3>
<p>✅ <strong>Best For:</strong></p>
<ul>
<li><p>Form validation (e.g., check if all fields are filled)</p>
</li>
<li><p>Checking conditions across UI components</p>
</li>
</ul>
<pre><code class="lang-javascript">javascriptCopyEditconst inputs = <span class="hljs-built_in">document</span>.querySelectorAll(<span class="hljs-string">"input"</span>);

<span class="hljs-keyword">const</span> allFilled = [...inputs].every(<span class="hljs-function"><span class="hljs-params">input</span> =&gt;</span> input.value.trim() !== <span class="hljs-string">""</span>);

<span class="hljs-keyword">if</span> (!allFilled) {
  alert(<span class="hljs-string">"Please fill in all fields!"</span>);
}
</code></pre>
<hr />
<h3 id="heading-flatmap-in-the-dom">🪜 <code>flatMap()</code> in the DOM</h3>
<p>✅ <strong>Best For:</strong></p>
<ul>
<li><p>Rendering deeply nested data structures into flat UI</p>
</li>
<li><p>Combining and flattening results (e.g., tags, categories)</p>
</li>
</ul>
<pre><code class="lang-javascript">javascriptCopyEditconst categories = [
  { <span class="hljs-attr">name</span>: <span class="hljs-string">"Fruits"</span>, <span class="hljs-attr">items</span>: [<span class="hljs-string">"Apple"</span>, <span class="hljs-string">"Banana"</span>] },
  { <span class="hljs-attr">name</span>: <span class="hljs-string">"Veggies"</span>, <span class="hljs-attr">items</span>: [<span class="hljs-string">"Carrot"</span>, <span class="hljs-string">"Spinach"</span>] },
];

<span class="hljs-keyword">const</span> allItems = categories.flatMap(<span class="hljs-function"><span class="hljs-params">cat</span> =&gt;</span> cat.items);

<span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">"#all-items"</span>).innerHTML = allItems
  .map(<span class="hljs-function"><span class="hljs-params">item</span> =&gt;</span> <span class="hljs-string">`&lt;li&gt;<span class="hljs-subst">${item}</span>&lt;/li&gt;`</span>)
  .join(<span class="hljs-string">""</span>);
</code></pre>
<hr />
<h2 id="heading-bonus-real-example-dynamic-accordion">💡 Bonus: Real Example — Dynamic Accordion</h2>
<p>Here’s how multiple methods might work together:</p>
<pre><code class="lang-javascript">javascriptCopyEditconst faqs = [
  { <span class="hljs-attr">question</span>: <span class="hljs-string">"What is HTML?"</span>, <span class="hljs-attr">answer</span>: <span class="hljs-string">"HTML is markup language."</span> },
  { <span class="hljs-attr">question</span>: <span class="hljs-string">"What is JS?"</span>, <span class="hljs-attr">answer</span>: <span class="hljs-string">"JS is a scripting language."</span> }
];

<span class="hljs-keyword">const</span> container = <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">"#faq"</span>);

container.innerHTML = faqs
  .map(<span class="hljs-function">(<span class="hljs-params">faq, index</span>) =&gt;</span> <span class="hljs-string">`
    &lt;div class="faq-item"&gt;
      &lt;button class="faq-question" data-index="<span class="hljs-subst">${index}</span>"&gt;<span class="hljs-subst">${faq.question}</span>&lt;/button&gt;
      &lt;div class="faq-answer hidden"&gt;<span class="hljs-subst">${faq.answer}</span>&lt;/div&gt;
    &lt;/div&gt;
  `</span>).join(<span class="hljs-string">""</span>);

<span class="hljs-built_in">document</span>.querySelectorAll(<span class="hljs-string">".faq-question"</span>).forEach(<span class="hljs-function"><span class="hljs-params">btn</span> =&gt;</span> {
  btn.addEventListener(<span class="hljs-string">"click"</span>, <span class="hljs-function">() =&gt;</span> {
    <span class="hljs-keyword">const</span> answer = btn.nextElementSibling;
    answer.classList.toggle(<span class="hljs-string">"hidden"</span>);
  });
});
</code></pre>
<hr />
<h2 id="heading-summary-table-for-dom-use">🚀 Summary Table for DOM Use</h2>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Method</td><td>DOM Use-case Examples</td></tr>
</thead>
<tbody>
<tr>
<td><code>forEach</code></td><td>Event listeners, class toggling</td></tr>
<tr>
<td><code>map</code></td><td>Generating innerHTML from data</td></tr>
<tr>
<td><code>filter</code></td><td>Showing filtered content (e.g., search, categories)</td></tr>
<tr>
<td><code>reduce</code></td><td>Totals, counting, grouping</td></tr>
<tr>
<td><code>find</code></td><td>Find data to populate forms or modals</td></tr>
<tr>
<td><code>some</code></td><td>Check if any input is invalid</td></tr>
<tr>
<td><code>every</code></td><td>Confirm all form fields are valid</td></tr>
<tr>
<td><code>flatMap</code></td><td>Flatten nested arrays for rendering</td></tr>
</tbody>
</table>
</div>]]></content:encoded></item></channel></rss>