Array-Flatten Array Javascript interview questions-1

imranmind

Imran shaikh

Posted on May 1, 2023

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.

💖 💪 🙅 🚩
imranmind
Imran shaikh

Posted on May 1, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related