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-lz1zq2 жыл бұрын
@@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-lz1zq2 жыл бұрын
@@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 ....
@sachinmalik58372 жыл бұрын
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-lz1zq2 жыл бұрын
Bro , when making recursive call to nth level ... why you didn't write base conditions to come back from last level of nesting ?
@PIYUSH-lz1zq2 жыл бұрын
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 ....
@saumyaranjannayak21012 ай бұрын
If someone finds reduce complex, try the below simple for loop soln: function flatten(arr, anss){ let ans = [] for(let i = 0; i
@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; }
@amansaxena44462 жыл бұрын
const res=[] function flat(arr){ for(let i=0;i
@PIYUSH-lz1zq2 жыл бұрын
Bro , when making recursive call to nth level ... why you didn't write base conditions to come back from last level of nesting ?
@amansaxena44462 жыл бұрын
@@PIYUSH-lz1zq else part will act as base cond, keeps on pushing and moving forward to next element
@PIYUSH-lz1zq2 жыл бұрын
@@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 ....
@pranavJha93Ай бұрын
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-lz1zq2 жыл бұрын
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 ....