Flatten Nested Arrays with JavaScript's flat() Method

💻 JavaScript Intermediate – Flatten a Nested Array Nested arrays can be tricky, but flattening them makes your data easier to work with. 📌 Problem: Convert a nested array into a single-level array. JavaScript code function flattenArray(arr) { return arr.flat(Infinity); } console.log(flattenArray([1, [2, [3, 4]], 5])); 📤 Output: [1, 2, 3, 4, 5] 📖 Explanation: • flat() method converts nested arrays into a single array. • Using Infinity ensures all levels of nesting are flattened. 💡 Tip: Great for working with complex JSON or API responses. #JavaScript #Coding #WebDevelopment #FrontendDevelopment #LearnToCode #ProgrammingTips

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories