Today I practiced an important JavaScript concept: Flattening a nested array manually using loops. Instead of using the built-in .flat() method, I implemented the logic myself to deeply understand how flattening actually works. 🧠 Problem Given this array: [1, [2, 3], [4, 5], 6] Return: [1, 2, 3, 4, 5, 6] 🔍 My Approach Created an empty result array. Looped through each element of the main array. Checked if the current element is an array using Array.isArray(). If it’s an array: Loop through it Push each inner element individually into result If it’s not an array: Push it directly into result 💡 Key Line That Does the Magic result.push(arr[i][j]); This line: Accesses elements inside the nested array Pushes them individually into the final result Removes one level of nesting 🎯 What I Learned How nested loops work in real problems The difference between pushing an array vs pushing its elements What “one-level flattening” actually means How .flat() works internally ⏱ Time Complexity O(n) — every element is visited once. Building fundamentals > memorizing shortcuts. Next step: Flatten deeply nested arrays using recursion 🔥 #JavaScript #FrontendDeveloper #ProblemSolving #WebDevelopment #100DaysOfCode #DSA #LearningInPublic

To view or add a comment, sign in

Explore content categories