Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
Convert multidimensional array to a string in javascript?

My input is a multidimensional array and I want everything in the array to be in one string -

Input -

let input = [[1,2,3],[1,3,2],[3,2,1],[3,1,2],[2,1,3],[2,3,,1]]

Expected Ouput

"123,132,321,312,213,231"

I have tried

input.join() // which gives me

"1,2,3,1,3,2,3,2,1,3,1,2,2,1,3,2,3,1"

And also I have tried

input.join('') // which gives me

"1,2,31,3,23,2,13,1,22,1,32,3,1"

I have also tried using a for loop for this

let input = [[1,2,3],[1,3,2],[3,2,1],[3,1,2],[2,1,3],[2,3,1]]
let output = ''

for (let i = 0; i < input.length; i++){
 let result = input[i]
 output += result.toString()
}

Which returns

 "123132321312213231"

I can't seem to crack this... Any tips??

Thanks

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Can be achieved simply using .map() with .join('') inside:

const input = [[1,2,3],[1,3,2],[3,2,1],[3,1,2],[2,1,3],[2,3,,1]];

const result = input.map(arr => arr.join('')).join();

console.log(result);

When you pass an empty string '' into .join(), all elements are joined without any characters in between them.

about 4 years ago · Juan Pablo Isaza Relatório

0

You almost there with your for loop

The other Solution from the User above me is in my opionion better

but here using your system you could do something like this:

let input = [[1, 2, 3], [1, 3, 2], [3, 2, 1], [3, 1, 2], [2, 1, 3], [2, 3, 1]]
let output = ''

// since we are working with an array why not use forEach, but you can also go with a normal for, or for of etc
input.forEach((array) => {
    // here we could another forEach for example
    array.forEach(number => { // adding all numbers inside the smaller array to one string
        output += number;
    })
    output += ','
})

output = output.slice(0, output.length - 1)

gives the output :

"123,132,321,312,213,231"
about 4 years ago · Juan Pablo Isaza Relatório

0

Array.prototype.join() is what you're looking for, but to join each array element without any separators requires an empty string array.join(""):

    let input = [[1,2,3],[1,3,2],[3,2,1],[3,1,2],[2,1,3],[2,3,1]]

    let stringified2DArray = ""

    for (let i in input) {
      if (i == input.length-1) stringified2DArray += input[i].join("")
      else stringified2DArray += input[i].join("") + ','
    }

    console.log(stringified2DArray)

In the above example, we loop through each inner array, join each element and add comma afterward. If it's the last iteration in the loop, we exclude the comma to avoid a trailing comma.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda