How do I output an array with nested arrays into a single string without commas?

let array = [
 ["1", "2"],
"3",
 ["4", ["5", "6"] ]
]

alert(array.toString())
// 1, 2, 3, 4, 5, 6

alert(array.join("))
// 1, 2 3 4, 5, 6
// A need: 1 2 3 4 5 6


I need the most simple way to take this array and nested arrays in one line without commas. toString displays with commas, Join not working with nested arrays
April 19th 20 at 12:22
3 answers
April 19th 20 at 12:24
Solution
const array = [
 ["1", "2"],
"3",
 ["4", ["5", "6"], ["7", "8"] ],
 ["9", "10", ["11", "42"]]
]

const res = array.flat(Infinity).join(" ");
console.log(res);
April 19th 20 at 12:26
Solution
alert(array.flat(Infinity).join(' '));
anyway, the comma is one in a three-dimensional array, I have an unlimited number of attachments - dax_Willm commented on April 19th 20 at 12:29
@dax_Willm, try again, I immediately noticed that you have over gaps - mervin commented on April 19th 20 at 12:32
@mervin, does not work, all the same, the comma between the 5 and 6 - dax_Willm commented on April 19th 20 at 12:35
@dax_Willm, and now?) - mervin commented on April 19th 20 at 12:38
April 19th 20 at 12:28
https://repl.it/repls/TurquoiseRottenEmbeds
function _(array) {
 const result = [];
 array.forEach(element => 
 Array.isArray(element) 
 ? result.push(..._(element)) 
 : result.push(element));
 return result;
}

let array = [
 ["1", "2"],
"3",
 ["4", ["5", "6"] ]
]

console.log(_(array).join(' '))
Fu fu fu - mervin commented on April 19th 20 at 12:31
@mervinwhat fu fu fu? Constructively please. - alysha.Ha commented on April 19th 20 at 12:34
@alysha.Ha, there is method flat(), which is already out of the box can more than your function with a strange name. So why complicate things? - mervin commented on April 19th 20 at 12:37
@mervin, there is a method, and you used the wrong. I solved the problem. The name is not strange, but with a hint that there is a built-in method "flat". - alysha.Ha commented on April 19th 20 at 12:40
@alysha.Ha, Oh well =) - mervin commented on April 19th 20 at 12:43

Find more questions by tags JavaScript