CrackFrontendCF
Resources
Practice
CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❀️ by Tushar Khanna

CrackFrontendCF
Resources
Practice

πŸ“‹ Array.prototype.flat() Polyfill

The flat() method creates a new array with all sub-array elements concatenated recursively up to the specified depth. Includes simple recursive and spec-compliant implementations.

The method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth. This polyfill provides both a simple recursive solution and a spec-compliant depth-aware implementation. -- βœ… Simple Recursive Implementation -- πŸ“Œ Example -- 🧠 Atom-of-Thoughts Breakdown 1. Base Case: If the item is not an array , push it to the result. 2. Recursive Case: If the item is an array , call on it, and concatenate its result. 3. Accumulator: Uses a local array to collect all flattened items. -- πŸ§ͺ Handles: Arbitrary nesting: βœ… Mixed types: βœ… Empty arrays: βœ… -- Here’s a spec-compliant polyfill for , matching ECMAScript behavior: -- βœ… Flat Polyfill (Depth-Aware) -- πŸ“Œ Usage -- 🧠 Atom-of-Thoughts Breakdown 1. Default for single-level flattening. 2. Recursive helper : If item is array and β†’ recurse with . Else β†’ push item to result. 1. Uses closure to accumulate flattened values. -- πŸ§ͺ Edge Case Behavior Output with with -- <!-quiz-start --Q1: What is the default depth value for ? [ ] 0 [x] 1 [ ] Infinity [ ] undefined Q2: What does return? [ ] [1, 2, 3, 4] [x] [1, 2, 3, [4]] [ ] [1, [2, [3, [4]]]] [ ] [1, 2, [3, [4]]] Q3: How does the recursive polyfill determine when to stop flattening? [ ] When the array is empty [x] When depth reaches 0 or the item is not an array [ ] When all elements are numbers [ ] When a null value is encountered <!-quiz-end --
JavaScriptPolyfills
🎯 Array.prototype.at() Polyfill
medium
βœ… Array.prototype.every() Polyfill
medium
πŸ”² Array.prototype.fill() Polyfill
medium
πŸ” Array.prototype.filter() Polyfill
medium
πŸ”Ž Array.prototype.find() Polyfill
medium
πŸ”’ Array.prototype.findIndex() Polyfill
medium
πŸ”™ Array.prototype.findLast() Polyfill
medium
πŸ”™ Array.prototype.findLastIndex() Polyfill
medium
πŸ“‹ Array.prototype.flat() Polyfill
medium
πŸ” Array.prototype.includes() Polyfill
medium
πŸ”’ Array.prototype.indexOf() Polyfill
medium
βœ… Array.isArray() Polyfill
medium
πŸ—ΊοΈ Array.prototype.map() Polyfill
hard
βž– Array.prototype.pop() Polyfill
medium
βž• Array.prototype.push() Polyfill
easy
πŸ”„ Array.prototype.reduce() Polyfill
medium
πŸ”„ Array.prototype.reverse() Polyfill
hard
⬅️ Array.prototype.shift() Polyfill
hard
πŸ”˜ Array.prototype.some() Polyfill
medium
πŸ”€ Array.prototype.sort() Polyfill
hard
➑️ Array.prototype.unshift() Polyfill
hard
πŸ“ž Function.prototype.apply() Polyfill
medium
πŸ”— Function.prototype.bind() Polyfill
hard
πŸ“ž Function.prototype.call() Polyfill
medium
9 of 24
LibraryJavaScriptPolyfills24 of 61

πŸ“‹ Array.prototype.flat() Polyfill

jspolyfillsmedium

The flat() method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth. This polyfill provides both a simple recursive solution and a spec-compliant depth-aware implementation.


βœ… Simple Recursive Implementation

function flattenRecursive(arr) {
  let result = [];

  for (const item of arr) {
    if (Array.isArray(item)) {
      result = result.concat(flattenRecursive(item)); // recurse
    } else {
      result.push(item); // base case
    }
  }

  return result;
}

πŸ“Œ Example

flattenRecursive([1, [2, [3, 4], 5], 6]);
// β†’ [1, 2, 3, 4, 5, 6]

🧠 Atom-of-Thoughts Breakdown

  1. Base Case:

    If the item is not an array , push it to the result.

  2. Recursive Case:

    If the item is an array , call flattenRecursive() on it, and concatenate its result.

  3. Accumulator:

    Uses a local result array to collect all flattened items.


πŸ§ͺ Handles:

  • Arbitrary nesting: βœ…
  • Mixed types: βœ…
  • Empty arrays: βœ…

Here’s a spec-compliant polyfill for Array.prototype.flat , matching ECMAScript behavior:


βœ… Flat Polyfill (Depth-Aware)

if (!Array.prototype.flat) {
  Array.prototype.flat = function(depth = 1) {
    const result = [];

    (function flatten(arr, d) {
      for (const item of arr) {
        if (Array.isArray(item) && d > 0) {
          flatten(item, d - 1);
        } else {
          result.push(item);
        }
      }
    })(this, depth);

    return result;
  };
}

πŸ“Œ Usage

[1, [2, [3, [4]]]].flat(2);
// β†’ [1, 2, 3, [4]]

🧠 Atom-of-Thoughts Breakdown

  1. Default depth = 1 for single-level flattening.
  2. Recursive helper flatten(arr, d) :
  • If item is array and depth > 0 β†’ recurse with depth - 1.
  • Else β†’ push item to result.
  1. Uses closure result[] to accumulate flattened values.

πŸ§ͺ Edge Case Behavior

InputOutput
[1, 2, [3]][1, 2, 3]
[1, [2, [3]]]with depth=1[1, 2, [3]]
[1, [2, [3]]]with depth=2[1, 2, 3]
[1, [2, [3]]]with depth=0[1, [2, [3]]]

Quick Quiz

Test your understanding with 3 quick questions

Q1What is the default depth value for `flat()`?
Q2What does `[1, [2, [3, [4]]]].flat(2)` return?
Q3How does the recursive `flat()` polyfill determine when to stop flattening?

Continue Reading

CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❀️ by Tushar Khanna