Flatten an array - frontend/Javascript Interview Question

preview_player
Показать описание
This video describes the questions that is repetitively asked in frontend interviews i.e. flatten a deep nested array and follows up that are asked after coding the solution.

Do let me know if you want me to cover some specific topics on frontend interview questions.
Рекомендации по теме
Комментарии
Автор

thank you, your teaching way is very simple and easy anyone can understand, i saw 4-5 videos regarding this question but didn't get, finally i saw this video and understand very easily, thank you again, you are doing very good

harshitsharma
Автор

You should put more content regarding these kinda tricky interview questions .

nixonnelson
Автор

easiest approach is

const ar=[1, 2, 3, 4, 5, [6, [7, 9, 8]]]


function flatten(ar, result=[]){
for(let i=0;i<ar.length;i++){
let value=ar[i];
if(Array.isArray(value)){
flatten(value, result);
}
else{
result.push(value);
}}
return result
}
console.log(flatten(ar));

srinathyadav
Автор

Thank you for helping with this. It really helps a novice programmer with using recursion.

kendallsflying
Автор

Fire solution and great vocalization of what you're doing. Thank you! <3

murcielago_software
Автор

had exactly same solution as you but stuck at 🚩 acc = acc.concat(flatten(item)) ... thanks a lot, that's what exactly I needed

BulletSP
Автор

Such a short simple and beginner friendly video. Thank you so much and you just got a new subscriber 😊

ankita_
Автор

Wow it was so good to explain. Thanks for this video..!

jitujahagirdar
Автор

Those videos are much helpful for us please make another 😔

ErJokerLover
Автор

Thanks for the video ☺️ and the great explanation

mrtaransraa
Автор

If values are only string, number or arrays you can just make the array string and split it by comma. arr = String(arr).split(', ')

mitkopetrov
Автор

thank you very much for solution but you can use only function reduce and add methode of : john(" " ) like this :
let flatten = myArray.reduce((acc, item) =>Array.isArray(item) ? acc.concat(item.join("")) : `${acc}${item}`, []);
console.log(flatten);

lyessaadoudi
Автор

const _ = require('lodash') // firstly install npm i lodash
const array = [1, 2, 3, [4, 5, [47, 48, 49], 6], 7, 8, [9, 10, 11]];
const newArray = _.flattenDeep(array)
console.log(newArray);

High_School_Developer
Автор

thanks for the video, even i missed the concat one and lost the interview :/

krishnachaitanya
Автор

You can use Array.flat method simply!!

rahulbhankar
Автор

The same question is asked by some interviewer. I missed this video 😟😟😟

mohd
Автор

Recursive Approach :
const array = [1, 2, 3, [4, 5, [47, 48, 49], 6], 7, 8, [9, 10, 11]];

function flatten(array){
function convert(arr){
arr.forEach((key, idx) => {
console.log(arr[idx]);
if(!Array.isArray(arr[idx])){
ans.push(arr[idx]);
}
else{
convert(arr[idx]);
}
})
}
let arr = [...array];
let ans = [];
convert(arr);
return ans;
}

let finalAns = flatten(array);
console.log(finalAns);

puneetpant
Автор

I'm getting an "acc is not iterable" error while typing out your exact code and trying to test it.

stevenedwards
Автор

I define by a nodejs
npm init
npm install
then dev i lodash

High_School_Developer
Автор

As expected there are 100 different ways to do things with JS (and coding in general). For simplicity however I think this video would've been better if explained using Mozilla's straight forward example:

const array = [1, 2, 3, [4, 5, 6], 7, 8, [9, 10, 11]];
console.log(array.reduce((acc, val) => acc.concat(val), []));

dylan
join shbcf.ru