Javascript logical questions

  1. Flat an array containing N number of nested arrays with any types of elements:
const list = [1,2,3,[4,5,6],7,8,[12,34,[55,66,77],51,34],{name: "Jagan"},56];
let newList = [];
const deepFlat = (list) => {
for(let n of list){
if(Array.isArray(n)){
deepFlat(n)
}else{
newList.push(n);
}
}
}
deepFlat(list)
console.log(newList)
// [ 1, 2, 3, 4, 5, 6, 7, 8, 12, 34, 55, 66, 77, 51, 34, { name: 'Jagan' }, 56 ]

--

--

Jagannath Swarnkar

Sr. Software Engineer | React js | Next js | Node Js | Flutter