Flatten Deeply Nested Array with Recursion in JavaScript

Day 26 of #30DaysOfJavaScript on LeetCode Today’s Challenge: 2625 – Flatten Deeply Nested Array Today’s problem was all about recursion and depth control. The task was to flatten a multi-dimensional array up to a given depth n, without using the built-in Array.flat() method. Here's my solution: var flat = function (arr, n) { if (n == 0) return arr; var ans = []; for (var i = 0; i < arr.length; i++) { if (Array.isArray(arr[i])) { ans.push(...flat(arr[i], n - 1)); } else { ans.push(arr[i]); } } return ans; }; 🔗 Try the problem here: https://lnkd.in/g6WC5mu7 #JavaScript #LeetCode #CodingChallenge #LearningJourney #WebDevelopment #Developers #FrontEndDevelopment #30DaysOfCode #30DaysOfJavaScript

  • text

To view or add a comment, sign in

Explore content categories