B
B
Banan442020-04-16 19:26:35
JavaScript
Banan44, 2020-04-16 19:26:35

How to display an array with nested arrays in one line, 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
// А нужно: 1 2 3 4 5 6


I need the simplest possible way to display an array and its nested arrays in one line without commas. toString prints with commas, Join doesn't work with nested arrays

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Tigran Abrahamyan, 2020-04-16
@Banan44

const array = [
    ["1", "2"],
    "3",
    ["4",  ["5", "6"], ["7", "8"] ],
    ["9", "10", ["11", "42"]]
]

const res = array.flat(Infinity).join(" ");
console.log(res);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question