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

153
Views
TypeError: no se pueden leer las propiedades de undefined (leyendo 'longitud'): me gustaría una explicación de por qué el código dice esto

Tarea: crear una función que elimine el elemento externo de una matriz si la matriz interna contiene un número determinado. es decir, filteredArray([[10, 8, 3], [14, 6, 23], [3, 18, 6]], 18) debe devolver [[10, 8, 3], [14, 6, 23]]

Me gustaría una explicación de qué está haciendo/leyendo exactamente el código al causar este error en lugar de solo una solución, si es posible.

He incluido mi proceso de pensamiento como notas en este código, así que espero que si me equivoco en alguna parte, se pueda señalar.


 function filteredArray(arr, elem) { let newArr = []; // Only change code below this line newArr = [...arr]; //copying the arr parameter to a new arr for (let i=0; i< newArr.length; i++){ //iterating through out array for (let x= 0; x< newArr[i].length; x++){ //iterating through inner array if(arr[i][x] === elem){ //checking each element of the inner array to see if it matches the elem parameter newArr.splice(i, 1); //if true, removing the entire outer array the elem is inside } } } // Only change code above this line return newArr; } console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));

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

0

Este error ocurre cuando intenta acceder a una propiedad en una variable que no está undefined .

Es probable que obtengas esto de tu línea:

 for (let x= 0; x< newArr[i].length; x++){

Si su argumento arr no es una matriz, newArr[0] no estará definido y, por lo tanto, `newArr[0].lenght generará este error.

about 4 years ago · Juan Pablo Isaza Report

0

Cuando está en la última iteración y encontró el valor, divide la matriz externa e itera aún la matriz interna, pero con el índice original de la matriz externa. Al reducir la longitud, ahora apunta sobre la longitud y cualquier intento de acceder a undefined con una propiedad sale mal.

Para superar esto y mantener los índices externos correctos, puede comenzar desde el último índice e iterar hacia atrás.

Además de eso, podría romper la búsqueda interna, porque al encontrar, no necesita más para iterar esta matriz.

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!