Write a recursive function flattenRecursively that flattens a nested array. Your function should be able to handle varying levels of nesting. code example
Example: Write a recursive function flattenRecursively that flattens a nested array. Your function should be able to handle varying levels of nesting.
function flatten(array) {
let flatArray = [];
for (let i = 0; i < array.length; i++) {
if (Array.isArray(array[i])) {
flatArray.push(...flatten(array[i]));
} else {
flatArray.push(array[i]);
}
}
return flatArray;
}
let array = [[1, 2, 3], [[4, 5], 6, [7, 8, 9]]];
console.log(flatten(array));
let array2 = [1, 2, [3, [4, 5, 6]]];
console.log(flatten(array2));