Polyfill in Javascript | Polyfill for Array flat method | Javascript Interview Questions

preview_player
Показать описание
Hey everyone, I have started a new polyfill series. These polyfills are asked in frontend interviews. I will be covering polyfills from basic to advanced. This video is on polyfill for array's flat method 😱.

So Stay tuned! And do watch this video till the end 🙏

Subscribe here 🤗 -

Timestamps -
0:00 Introduction
0:15 What is Array's flat method?
1:30 Writing Polyfill
5:40 Understanding recursion by doing dry run
6:55 Dry run ppt

Link to other videos 📚
Polyfill for filter and reduce -
Polyfill for map and foreach -
Frontend Interview Experience -
Frontend System Design 🧑‍💻 -
MMT Interview -
Top DSA Questions -

#react #javascript #frontend
Рекомендации по теме
Комментарии
Автор

Hey everyone, don' forget to like share and subscribe 🙏 Thank you for watching ❤

akashingole
Автор

Hi Akash, there is a small note
if the array is nested multiple times then depth condition has to be removed.

Array.prototype.myflat = function(){
let res = [];
this.forEach((el)=>{
if(Array.isArray(el) ){
res.push(...el.fullFlat());
} else {
res.push(el)
}
});
return res;
}

Noushad-ps
Автор

Thanks for sharing the polyfill series . Very helpful 💯👍

adityagoswami
Автор

please add a default value of 1 to depth param to align with original flat method.

softwareengineering
Автор

dont understood working (depth-1) working

stoker
Автор

Array.prototype._flat = function (level = 1) {
if (!Array.isArray(this)) {
throw new Error(`${this}.flat is not a function`);
}
return (function getFlatArr(arr, currentLevel) {
let result = [];
let depth = 0;
arr.forEach((element) => {
if (Array.isArray(element) && depth < currentLevel) {
depth++;
result = result.concat(getFlatArr(element, --currentLevel));
} else {
result.push(element);
}
});
return result;
})(this, level);
};

coolme