Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
Cómo convertir un archivo de texto en un objeto de par de valores clave

Tengo un archivo de texto cuyo contenido se ve así

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

Quiero convertir este archivo/cadena de texto en un objeto javascript para que coincida con este formato

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

Por favor, ayúdame

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede dividir la cadena por coma, filtrar las cadenas sin el símbolo igual, filtrar las claves "desconocidas" y finalmente construir el objeto deseado usando la función Array.prototype.reduce .

Se debe utilizar la siguiente lógica para cada línea del archivo.

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!