I have been working on solving this challenge using recursion because I enjoy the challenge. The task at hand involves taking an array of arrays and transforming it into a single array with all the values combined. While I have made good progress, I am encountering an issue where the new array I am appending to keeps resetting after each recursion. I would greatly appreciate any advice or suggestions to help me overcome this obstacle.
var multiArray = [[1, 2],[3, 4, 5], [6, 7, 8, 9]]
const flattenArrays = function (arr) {
let result = [];
arr.map(element => {
if (Array.isArray(element)) {
console.log('Is Array ---> ', element)
flattenArrays(element);
} else {
console.log('Result ----->', result)
console.log('Else --->', element)
result.push(element);
}
});
return result;
};
console.log('Result ----->', flattenArrays(multiArray)); //[ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]