My solution without using nested function in flatten by utilizing prototype method defined for array. function flatten(){ const arr=[]; this.map(x=>{ if(Array.isArray(x)) arr.push(...x.flatten()); else arr.push(x); }) return arr; } Array.prototype.flatten=flatten; Great explaination btw. Keep posting such awesome content.
Nice. Clean approach. Thanks for sharing! Only point would be that this approach would be a tad bit slower than mentioned in the video as on every iteration (array elements), we would be creating a new array and spreading the output. Hence, more operations to perform. Still a good solution. :D
Ans for the follow up question (flatten to a certain deep ) :- function flatten(deep) { // write your code here let output = []; function processing(arr,deep) { for(let i = 0; i < arr.length ; i++ ) { const current = arr[i]; if(Array.isArray(current) && deep > 0) { processing(current,deep - 1); } else { output.push(current); } } }
As always , i again fall in love with your explanation , people who are missing thia channel is loosing out so much, i am eagerly waiting to connect with you yomesh bhaiya
My answer for flattening with depth: Array.prototype.flattenDepth = function (depth) { let flatArray = []; for (let i = 0; i < this.length; i++) { if (depth && Array.isArray(this[i])) { const returned = this[i].flattenDepth(depth - 1); flatArray = flatArray.concat(returned); } else { flatArray.push(this[i]); } } return flatArray; }
Hi Yomesh, Thanks for explaining it. Can you make a detailed video on - Time ago functionality in javascript. I mean how diff ways and in min lines in javascript we can make this functionality.
@@megheshshenoy By depth he meant the position of the element in the given array. for ex: the first three elements 1,2,3 are i just in input array so its depth is 0 but element 4 is inside another array so its depth is 1 and so on depth of element is based on where the element is positioned from the given array
Good content brother .. I have a question here ... the last statement of the problem says it has to be invoked existing and earlier array .. so if you try to invoke the function Input.flatten() before line 78 it will throw an error since the function flatten is not assigned yet ... so we have to use IIFE on the Array.prototype.flatten = flatten right ?
Devtools Tech comprises of 3 like-minded engineers. Other two are my fellow amazing content creators: Saloni Kathuria and Puneet Ahuja. Check-out and subscribe to our channel for their useful videos! ~ Yomesh Gupta
How about this as a solution? Would love to know your thoughts Yomesh and others. Array.prototype.flatten = function () { let flatArray = []; for (let i = 0; i < this.length; i++) { if (Array.isArray(this[i])) { const returned = this[i].flatten(); flatArray = flatArray.concat(returned); } else { flatArray.push(this[i]); } } return flatArray; }
My solution without using nested function in flatten by utilizing prototype method defined for array.
function flatten(){
const arr=[];
this.map(x=>{
if(Array.isArray(x)) arr.push(...x.flatten());
else arr.push(x);
})
return arr;
}
Array.prototype.flatten=flatten;
Great explaination btw. Keep posting such awesome content.
Nice. Clean approach. Thanks for sharing!
Only point would be that this approach would be a tad bit slower than mentioned in the video as on every iteration (array elements), we would be creating a new array and spreading the output. Hence, more operations to perform. Still a good solution. :D
@@DevtoolsTech yeah that's a catch. Thanks for pointing this out.
Ans for the follow up question (flatten to a certain deep ) :- function flatten(deep) {
// write your code here
let output = [];
function processing(arr,deep) {
for(let i = 0; i < arr.length ; i++ ) {
const current = arr[i];
if(Array.isArray(current) && deep > 0) {
processing(current,deep - 1);
} else {
output.push(current);
}
}
}
processing(this,deep);
return output;
}
Array.prototype.flatten = flatten;
really helpful, Great Content !
Thanks a lot
I never thought of the 2nd solution, really impressive! Great Content !
Thank you. Glad you liked it. 😄
As always , i again fall in love with your explanation , people who are missing thia channel is loosing out so much, i am eagerly waiting to connect with you yomesh bhaiya
Thanks ... More such problems bro .. going great
sir , can u make videos to make nested comment section and folder structure using JS
My answer for flattening with depth:
Array.prototype.flattenDepth = function (depth) {
let flatArray = [];
for (let i = 0; i < this.length; i++) {
if (depth && Array.isArray(this[i])) {
const returned = this[i].flattenDepth(depth - 1);
flatArray = flatArray.concat(returned);
} else {
flatArray.push(this[i]);
}
}
return flatArray;
}
You can create a repo create a readme or file for every question and we can submit our solution for that question?
How is the idea?
Sounds like a good idea. Will look into this! :D
Hi Yomesh,
Thanks for explaining it. Can you make a detailed video on - Time ago functionality in javascript.
I mean how diff ways and in min lines in javascript we can make this functionality.
Great suggestion!
Thanks this was asked in one good product base company :(
Which one?
Thanks! Great explanation👍
You are welcome!
Got the same question for one online interview for Front end round
ohh great! Do share your solution with us all! :D
Hi Yomesh!Can you please give an example for the depth Search?
Hi, thank you for your comment. Do you mean Depth-first search? Like traversing a tree?
@@DevtoolsTech No Like You Told RIght In the Video if we pass a parameter to flatten indicating depth .As Told in Video 16:22
@@megheshshenoy By depth he meant the position of the element in the given array. for ex: the first three elements 1,2,3 are i just in input array so its depth is 0 but element 4 is inside another array so its depth is 1 and so on depth of element is based on where the element is positioned from the given array
Good content brother .. I have a question here ... the last statement of the problem says it has to be invoked existing and earlier array .. so if you try to invoke the function Input.flatten() before line 78 it will throw an error since the function flatten is not assigned yet ... so we have to use IIFE on the Array.prototype.flatten = flatten right ?
Yes, this is more like Polyfill so these get included before the source code. So, that function is assigned before usage.
Or you could demostrate them your ES2019 skills and make it happen by
Array.prototype.flatten = function() {
return this.flat(Infinity)
}
Yes, ofcourse. Idea was discuss how to implement this and understand different concepts via doing so.
Who are others two, in your thumbnail?
Devtools Tech comprises of 3 like-minded engineers. Other two are my fellow amazing content creators: Saloni Kathuria and Puneet Ahuja. Check-out and subscribe to our channel for their useful videos!
~ Yomesh Gupta
flatter than the flat earth society
How about this as a solution? Would love to know your thoughts Yomesh and others.
Array.prototype.flatten = function () {
let flatArray = [];
for (let i = 0; i < this.length; i++) {
if (Array.isArray(this[i])) {
const returned = this[i].flatten();
flatArray = flatArray.concat(returned);
} else {
flatArray.push(this[i]);
}
}
return flatArray;
}