this will not work if you run this function for [ 'hello, world',['something,test'] ] => since you are splitting the converted arr string using comma (,) resulted output will be ['hello', 'world','something','test']
@PIYUSH-lz1zq Жыл бұрын
@@akshaykumar5012 while pushing the actual output to res after that we should empty the curr array so taht fresh val dont append with previous values ...... what we used to do in c++/java !!! please tell ....
@PIYUSH-lz1zq Жыл бұрын
@@yzo954 while pushing the actual output to res after that we should empty the curr array so taht fresh val dont append with previous values ...... what we used to do in c++/java !!! please tell ....
@sachinmalik5837 Жыл бұрын
Hi, Very nice explanation for the flow. I am commenting a solution without Reduce so if someone doesn't fully understand Reduce can still do this one of the most famous frontend question const flattenArray = (intialArray) => { var result = []; for(let i=0;i
@PIYUSH-lz1zq Жыл бұрын
Bro , when making recursive call to nth level ... why you didn't write base conditions to come back from last level of nesting ?
@PIYUSH-lz1zq Жыл бұрын
while pushing the actual output to res after that we should empty the curr array so taht fresh val dont append with previous values ...... what we used to do in c++/java !!! please tell ....
@NikhilSharma-r1v Жыл бұрын
function flattenArr(arr) { const result = []; arr.forEach((element) => { if (Array.isArray(element)) { const nestedFlatArr = flattenArr(element); result.push(...nestedFlatArr); } else { result.push(element); } }); return result; }
@amansaxena4446 Жыл бұрын
const res=[] function flat(arr){ for(let i=0;i
@PIYUSH-lz1zq Жыл бұрын
Bro , when making recursive call to nth level ... why you didn't write base conditions to come back from last level of nesting ?
@amansaxena4446 Жыл бұрын
@@PIYUSH-lz1zq else part will act as base cond, keeps on pushing and moving forward to next element
@PIYUSH-lz1zq Жыл бұрын
@@amansaxena4446 while pushing the actual output to res after that we should empty the curr array so taht fresh val dont append with previous values ...... what we used to do in c++/java !!! please tell ....
@pranavJha9326 күн бұрын
let myArr = []; function flat(element){ for(const el of element){ if(typeof el ==='object'){ flat(el); } else myArr.push(el); } } flat(arr)
@venkateshpachigulla2 жыл бұрын
We can achieve this using Array flat method like console.log(Array.flat(arr, Infinity)). We can consider your code as a polyfill for Array flat method.
@js_cafe2 жыл бұрын
Yes, but refrain from using flat method in interview, as the interviewer will not be seeking you to use in built helper functions.
@PIYUSH-lz1zq Жыл бұрын
while pushing the actual output to res after that we should empty the curr array so taht fresh val dont append with previous values ...... what we used to do in c++/java !!! please tell ....