DEV Community

Cover image for Array-Flatten Array Javascript interview questions-1
Imran shaikh
Imran shaikh

Posted on

Array-Flatten Array Javascript interview questions-1

Frontend Interview or Nodejs Interview it's the most frequently asked interview question.

function doFlat(){
    let flatten = [];
    function flat(arr){    
        for(let i=0; i<arr.length; i++){
            const item = arr[i];
            if(Array.isArray(item)){
                // Object.assign(flatten,flat(item));
               [...flatten,flat(item)]
            }else{
                flatten.push(item);
            }
        }
        return flatten;
    }
    return {
        flat
    }
}

const {flat} = doFlat();

console.log(flat([1,2,[3,[0,4,5],4,[5,6]]]))
Enter fullscreen mode Exit fullscreen mode

If you want to see more interview questions please reach out my GitHub profile

https://github.com/imran-mind/javascript-notes/blob/master/JS-Interview-coding-ques/

If you find this blog helpfull, please like and comment and don't forget to share this.

Top comments (0)