Flat nested array without using built-in methods. Recursive program example
const arr = [1,2,[3,4],5,[6,[7,8],9],10];
function flatArray(input,output){
input.forEach( (value)=>{
if(typeof value== 'object'){
flatArray(value,output);
}else{
output.push(value);
}
});
return output;
}
console.log(flatArray(arr,[]));
Top comments (5)
This is sample example,not using built in methods. Way to learn coding :)
Nice recursive code! As a side-note, this is built into the JS standard:
developer.mozilla.org/en-US/docs/W...
Why creating a local?
Also interesting name xValue?
I once had a college who named all his variables x1, x2, ....
Cleaned up the code . Thanks for the feedback :)