FlareStart
HomeNewsHow ToSources
FlareStart

Where developers start their day. All the tech news & tutorials that matter, in one place.

Quick Links

  • Home
  • News
  • Tutorials
  • Sources
  • Privacy Policy

Connect

© 2026 FlareStart. All rights reserved.

Back to articles
Flatten Deeply Nested Array
How-ToWeb Development

Flatten Deeply Nested Array

via Dev.to JavaScriptKush Bhandari19h ago

let arrItem=[1,[2,3,[4,5],[5,[3,[29]]]]]; I was asked in the interview to flatten this array meaning convert this array into something like this [1,2,3,4,5,5,3,29]; So, my first approach was to use a inbuild flat method of array. arrItem.flat() Now, the problem with this is it doen't flat deeply nested array. So you have to provide the depth. Here we can provide depth as infinity. So the answer would be. arrItem.flat(Infinity) Now, the interviewer will ask for a custom implementation of flat method. There are two approach to do so. 1) Recursive Approach Array . prototype . flattenArray = function (){ // This is recursion Approach let array = this ; let output = []; function flatArray ( array ){ for ( let item of array ){ if ( Array . isArray ( item )){ flatArray ( item ) } else { output . push ( item ) } } } flatArray ( array ) return output ; } 2) Using Stack let arrItem = [ 1 ,[ 2 , 3 ,[ 4 , 5 ],[ 5 ,[ 3 ,[ 29 ]]]]]; Array . prototype . flattenArray = function ( depth = 1 ){ let stac

Continue reading on Dev.to JavaScript

Opens in a new tab

Read Full Article
3 views

Related Articles

I Ran the Same C Code on Multiple Compilers… and Got Strange Results
How-To

I Ran the Same C Code on Multiple Compilers… and Got Strange Results

Medium Programming • 11h ago

The Inheritance Trap: How to Avoid Fragile Base Classes
How-To

The Inheritance Trap: How to Avoid Fragile Base Classes

Medium Programming • 12h ago

Eighty Years Later, the Chemex Still Makes Better Coffee
How-To

Eighty Years Later, the Chemex Still Makes Better Coffee

Wired • 13h ago

The Day I Realized Coding Is Less About Computers and More About Learning How Humans Think
How-To

The Day I Realized Coding Is Less About Computers and More About Learning How Humans Think

Medium Programming • 13h ago

The Strange Advice Engineers Eventually Hear
How-To

The Strange Advice Engineers Eventually Hear

Medium Programming • 17h ago

Discover More Articles