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

118
Visualizações
How do I turn this string into an array of numbers?

I'm writing this code where a user inputs an array in a input box, so the box pretty much contains a string that looks somewhat like this: [2, 2, 2, 2, 2, 9, 9, 9, 2, 2, 2], [2, 1, 1, 1, 1, 9, 9, 9, 1, 1, 2], [2, 1, 1,14, 1, 9, 9, 9, 1, 1, 2] etc. etc.

I want to convert this into an array of arrays of numbers so that my code can do something with this, but I'm not sure how.

Simply grabbing the value of the input box outputs something like this: '[2, 2, 2, 2, 2, 9, 9, 9, 2, 2, 2], \n[2, 1, 1, 1, 1, 9, 9, 9, 1, 1, 2], \n[2, 1, 1,14, 1, 9, 9, 9, 1, 1, 2]', and when I attempt to split it/regex replace something, either I get nothing, or I get arrays that look like this: ["2, 2, 2, 2, 2, 9, 9, 9, 2, 2, 2"]. I'm not quite sure how to fix this, and the code I am using needs it as numbers.

I don't know what to do, so any ideas would be helpful. Thanks!

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

0

You can use JSON.parse:

const str = '[2, 2, 2, 2, 2, 9, 9, 9, 2, 2, 2],     \n[2, 1, 1, 1, 1, 9, 9, 9, 1, 1, 2],     \n[2, 1, 1,14, 1, 9, 9, 9, 1, 1, 2]';

const arrs = JSON.parse('[' + str + ']')

console.log(arrs)

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