<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shamun Khatri</title>
    <description>The latest articles on DEV Community by Shamun Khatri (@shamun_khatri).</description>
    <link>https://dev.to/shamun_khatri</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2229158%2Fba987870-4faa-4144-9f1f-914d6e5692f3.png</url>
      <title>DEV Community: Shamun Khatri</title>
      <link>https://dev.to/shamun_khatri</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shamun_khatri"/>
    <language>en</language>
    <item>
      <title>Implement Array.prototype.flat() Solution</title>
      <dc:creator>Shamun Khatri</dc:creator>
      <pubDate>Mon, 09 Dec 2024 10:56:26 +0000</pubDate>
      <link>https://dev.to/shamun_khatri/implement-arrayprototypeflat-solution-a2b</link>
      <guid>https://dev.to/shamun_khatri/implement-arrayprototypeflat-solution-a2b</guid>
      <description>&lt;p&gt;Implementing &lt;code&gt;Array.prototype.flat()&lt;/code&gt; Using an Iterative Approach&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;br&gt;
The JavaScript method Array.prototype.flat() is used to flatten nested arrays up to a specified depth. If no depth is provided, it defaults to flattening one level deep. In this guide, we’ll implement the flat() method using an iterative approach with a stack, providing an efficient and readable solution.&lt;/p&gt;

&lt;p&gt;We can solve this problem by both iterative and recursive methods but here I am only implementing the iterative way of solving it.&lt;/p&gt;

&lt;p&gt;Most of the solutions on the internet are not the proper solution that handles sparse arrays and undefined values. There may be better solutions available but this works for me and the comment section is open if you know the better way.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem&lt;/strong&gt;&lt;br&gt;
source - &lt;a href="https://bigfrontend.dev/problem/implement-Array-prototype.flat" rel="noopener noreferrer"&gt;https://bigfrontend.dev/problem/implement-Array-prototype.flat&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem Description&lt;/strong&gt;&lt;br&gt;
Given a nested array, we need to flatten it up to a specified depth.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr = [1, [2], [3, [4]]];

flat(arr);
// [1, 2, 3, [4]]

flat(arr, 1);
// [1, 2, 3, [4]]

flat(arr, 2);
// [1, 2, 3, 4]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Iterative Solution with Stack&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**
 * @param { Array } arr
 * @param { number } depth
 * @returns { Array }
 */
function flat(arr, depth = 1) {
  const flatArray = [];
  const stack = [];
  for (const index in arr) {
    stack.push([arr[index], depth]);
  }

  while (stack.length &amp;gt; 0) {
    let lastElement = stack[stack.length - 1];
    if (!lastElement) {
      stack.pop();
      flatArray.push(lastElement);
      continue;
    }
    const [item, depth] = stack.pop();
    if (Array.isArray(item) &amp;amp;&amp;amp; depth &amp;gt; 0) {
      for (const index in item) {
        stack.push([item[index], depth - 1]);
      }
    } else {
      flatArray.push(item);
    }
  }

  return flatArray.reverse();
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This implementation efficiently flattens arrays providing a clean, scalable solution. Let me know if you'd like further enhancements! 🚀&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
