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

140
Views
¿Cómo presionar la tecla como valor iterativo en js?

Hola, tengo un bucle de dos for que contiene la verificación de la longitud de cada bucle y empuja el par de valores clave de la matriz

 const reader = new FileReader(); // filereader reader.readAsText(e.target.files[0]); // read as text reader.onload = () => { const allText = reader.result; var allTextLines = allText.split(/\r\n|\n/); var headers = allTextLines[0].split('\t'); var lines = []; for (var i=1; i<allTextLines.length; i++) { var data = allTextLines[i].split('\t'); if (data.length == headers.length) { var tarr = []; for (var j=0; j<headers.length; j++) { tarr.push(headers[j]+":"+data[j]); } lines.push(tarr); } }

y mi salida está por debajo

 [Array(2),Array(2)] output now i'm getting [ [ 0:'product:PA123' 1:'value:123' 2:'price:$234' ], [ 0:'product:PA234' 1:'value:235' 2:'price:$24' ], ] expected output [ [ product:PA123, value :123, price :234 ], [ product:PA123, value :123, price :234 ] ]

Necesito insertar encabezados [j] como clave, pero no puedo. ¿Cómo hacerlo en JS? Soy literalmente nuevo. por favor desnudame. Gracias

disculpe por el mal formato.

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

0

Editado: ya obtienes el resultado esperado. simplemente no se confunda con las claves de índice 0, 1, 2. La consola de desarrollo muestra el índice de la matriz como clave para que sea más legible. Todavía estás confundido solo con el resultado de la consola.

 console.log(JSON.stringify(lines));

Para salida como matriz de modelo: es decir

 [ { product:PA123, value :123, price :234 }, { product:PA123, value :123, price :234 } ] // code for (var i=0; i<allTextLines.length; i++) { var data = allTextLines[i]; if (data.length == headers.length) { var model= {}; for (var j=0; j<headers.length; j++) { model[headers[j]] = data[j]; } lines.push(model); } } console.log(JSON.stringify(lines));

Para salida como matriz de matriz de cadenas: es decir

 [ [ "product:PA123", "value :123", "price :234" ], [ "product:PA123", "value :123", "price :234" } ] //code for (var i=0; i<allTextLines.length; i++) { var data = allTextLines[i]; if (data.length == headers.length) { var stringArray= []; for (var j=0; j<headers.length; j++) { stringArray.push(headers[j]+":"+data[j]); } lines.push(stringArray); } } console.log(JSON.stringify(lines));

Espero que esto responda a su consulta.

about 4 years ago · Juan Pablo Isaza Report

0

for (var i=0; i<allTextLines.length; i++) { var data = allTextLines[i]; if (data.length == headers.length) { var tarr = []; for (var j=0; j<headers.length; j++) { let a = headers[j]; let b = {} b[headers[j]] = data[j]; tarr.push(b)**** } lines.push(tarr); } }
about 4 years ago · Juan Pablo Isaza Report

0

Por favor cambie la siguiente línea a esto.

:de

 tarr.push(headers[j]+":"+data[j])

: a

 tarr.push({[headers[j]]:data[j]})
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!