+ 1

Remove the [] from given Array in Javascript?

One MNC Interviewer asked this question Var a = [1,[2,3],4] but we need output is [1,2,3,4]...Please someone explain how to remove the [] inbetween the given Array in Javascript

7th Jul 2018, 2:43 PM
Suren Messi
Suren Messi - avatar
4 Answers
+ 8
Array.prototype.flat() the flat() method reduces multidimensional arrays to just...1 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat
7th Jul 2018, 3:38 PM
᠌᠌Code X
᠌᠌Code X - avatar
+ 5
Use : a= [].concat(...a); //Flats one dimension Brains🇳🇬 cant find Array.prototype.flatten 😐 Mohamed ELomari I don't think the interviewer would be asking him about a Stage 3 ES10/ES2019 proposal.
8th Jul 2018, 12:31 PM
Swapnil Srivastava
Swapnil Srivastava - avatar
+ 1
Thank you
8th Jul 2018, 9:25 AM
Suren Messi
Suren Messi - avatar