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

157
Views
Llamar al índice de una matriz en bucle que causa un error

Así que básicamente importo un archivo JSON. Obtengo muchas matrices y cada matriz tiene 4 elementos. Quiero analizar el tercer elemento de cada matriz en su propia matriz de variables.

 $("#nextQ").click(function() { var Quotes = []; var totalQ //The total number of available quotes to choose from //Get quotes from JSON file $.ajax({ url: '../facts.json', datatype: 'json', type: 'get', success: function(data) { console.log(data[0][2]); //This WORKS console.log(data.length); //Returns 64 totalQ = data.length; for (i = 0; i <= totalQ; i++) { Quotes[i] = data[3][2]; //This WORKS Quotes[i] = data[i][2]; //This gives ERROR } } }); });

Cuando uso data[i][2] me sale este error: Uncaught TypeError: Cannot read property '2' of undefined . Sin embargo, este error no ocurre si uso data[6][2] o cualquier otro número.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Debe actualizar la condición del bucle for desde i <= totalQ; a i <totalQ; , ya que el índice comienza desde 0

 for (i = 0; i < totalQ; i++) { Quotes[i] = data[i][2]; }

O puede usar $.each() como sugirió @adeneo

 $.each(data,function(i,v){ Quotes[i] = v[2]; })

O puede usar el map()

 Quotes = data.map(function(v){ return v[2]; })
over 4 years ago · Santiago Trujillo 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!