I am trying to solve this problem with recursion because I want to make my life worse.
(我想通过递归来解决此问题,因为我想让自己的生活变得更糟。)
I am taking an array of arrays and returning one array with all of the values. (我正在使用一个数组数组,并返回一个包含所有值的数组。)
It is so close to working but the new array I am pushing to keeps reseting after every recursion. (它是如此接近工作,但是我要推送的新数组在每次递归后都会不断重置。)
Can I get some advice? (我可以得到一些建议吗?)
var myArray = [[1, 2],[3, 4, 5], [6, 7, 8, 9]]
const flatten = function (arr) {
let output = [];
arr.map(element => {
if (Array.isArray(element)) {
console.log('Is Array ---> ', element)
flatten(element);
} else {
console.log('Output ----->', output)
console.log('Else --->', element)
output.push(element);
}
});
return output;
};
console.log('Retrun ----->', flatten(myArray)); //[ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
ask by Jkaram translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…