Back to articles
Array flattening in javaScript (basic to advance)

Array flattening in javaScript (basic to advance)

via Dev.to JavaScriptAbhishek sahni

Before taking the dive into the concept of array flattening let's understand what are nested arrays. Nested arrays are nothing just an array inside another array. Nested arrays are used to handle complex data in organise way that cannot be represented by a linear array. Code Example : const numbers = [ 1 , 2 , [ 3 , 4 ], 5 ]; console . log ( numbers [ 2 ]); // [3, 4] console . log ( numbers [ 2 ][ 0 ]); // 3 console . log ( numbers [ 2 ][ 1 ]); // 4 how above code work index 2 gives -> [ 3 , 4 ] [2][0] gives -> 3 So , now we understand nested arrays but what is array flattening ? Array flattening is a process to combine multi-dimensional (nested) arrays by extracting all sub-arrays into a singe main array. what is the use case of array flattening ? Api response : when real world data arrive from sever it may be a nested array. So we combine multiple array into one single array to simplify the response. const users = [ { name : " Abhi " , skills : [ " HTML " , " CSS " ] }, { name : " Ra

Continue reading on Dev.to JavaScript

Opens in a new tab

Read Full Article
2 views

Related Articles