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

177
Visualizações
How to convert text file to a key value pair object

I have a text file which content looks like this

textFile = "NGGCVS3.scratch1.create,end_date="07/03/2025",pin="31742",scnum="2736289877",senum="250322602004999",SCRPREF="500",SUSP_F="0""

I want to convert this text file / string into a javascript object to match this format

const object = {
 pin: '31742',
 scnum: '2736289877',
senum: "250322602004999"

}

Please help me out

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

0

You can split the string by comma, filter out those string without the equal symbol, filter out the "unkown" keys and finally build the desired object by using the function Array.prototype.reduce.

The following logic should be used for each line in the file.

const textFile =  `NGGCVS3.scratch1.create,end_date="07/03/2025",pin="31742",scnum="2736289877",senum="250322602004999",SCRPREF="500",SUSP_F="0"
NGGCVS3.scratch1.create,end_date="07/03/2025",pin="31742",scnum="2736289877",senum="250322602004999",SCRPREF="500",SUSP_F="0"`;
const keys = ["pin", "scnum", "senum"];
const result = textFile.split("\n").map(line => {
    return line.split(",")
             .map(str => str.split("="))
             .filter(({length}) => length > 1)
             .map(([key, value]) => ({[key]: value.replace(/"/g, '')}))
             .filter((obj) => Object.keys(obj).some(key => keys.includes(key)))
             .reduce((a, c) => ({...a, ...c}), {})
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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