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

163
Visualizações
How to make array from input

I have array in txt file and read this with

   fs.readFileSync("./input.txt")

When i wrap it in console.log i get(since it is written in the file itself):

    1 2 3 
    100 5000 

I would have the array:

['1','2','3','100','5000']

Placement of the array in the input file should not change. Suggest how to do it, please.

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

0

You can use regex to split words: \w+

let a = `    1 2 3 
    100 5000 `;
    
console.log(a.match(/\w+/g))

To read your file and split it:

fs.readFileSync("./input.txt").match(/\w+/g)
about 4 years ago · Juan Pablo Isaza Relatório

0

One way, load it then, split by lines, then on each line split by space then flatten it, then filter out empty values.

let str = `1 2 3 
    100 5000 0`; // added 0 to show filter(Boolean) wont remove
    
console.log(str.split('\n').map(v => v.split(' ')).flat().filter(Boolean))

Result:

[
  "1",
  "2",
  "3",
  "100",
  "5000",
  "0"
]
about 4 years ago · Juan Pablo Isaza Relatório

0

You could split the string by a space, then filter out the falsy items in the resulting array.

const input = `    1 2 3 
    100 5000 `;

let res = input.split(" ").filter(e => e.trim())
console.log(res)

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