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

244
Views
No se pueden leer las propiedades de undefined (leyendo 'split')

Escribí un código para crear matrices de objetos en archivos js basados en datos en archivos txt de la siguiente manera:

 var num1 = ['100', '200', '500', '1000'] var num2 = ['10', '20', '30', '40', '50'] for (var i of num1) { for (var j of num2) { var source = './dat/txt/data_' + i + '_' + j + '.txt' var dest = './dat/js/data_' + i + '_' + j + '.js' var write = '[\n' fs.readFile(source, 'utf-8', function (err, data) { if (err) { console.log(err) } var datArray = data.split('\r\n') for(let k=0;k<parseInt(i);k++){ var numbers = datArray[k].split(' ') write = write.concat('{\npos:\nx:',numbers[0],',\ny:',numbers[1],'\n},\nbeta:',numbers[2],'\n},') } }) fs.writeFile(dest, write, (err) => { if (err) { console.log(err) } else console.log('Written successfully') }) } }

Los archivos txt tienen el siguiente formato:

 1 2 3 1 2 3 1 2 3 ....

Y quiero crear matrices de objetos de la siguiente manera:

 [ { pos: { x: 1, y: 2, }, beta: 3 }, ... ]

Cuando ejecuto el código, tiene este error:

 var numbers = datArray[k].split(' ') TypeError: Cannot read properties of undefined (reading 'split')

Intenté registrar la matriz datArray y es una matriz de cadenas normal, y la longitud de datArray es igual al valor de i. Los archivos de salida también solo escriben un solo carácter '[' y un salto de línea. ¿Qué estoy haciendo mal aquí?

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

0

Creo que para resolver este problema cambia esta línea:

 var numbers = datArray[k].split(' ')

con este :

 var numbers = datArray[k]?.split(' ')
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!