Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

286
Vistas
No se pueden leer las propiedades de undefined (leyendo 'tamaño')

Tengo 2 matrices. Estoy usando un ciclo for en arr1 y comprobando si arr2 contiene algunos de los mismos tamaños de zapatos que en arr1. Si la condición verifica que un tamaño de zapato de arr1 no existe en arr2, entonces empujará un objeto ( {size: '-'} ) a newArr, si existe, simplemente empujará el objeto de zapato de arr2 a newArr

código:

 const newArr = [] const arr1 = [ { size: 'US 3' }, { size: 'US 4' }, { size: 'US 5' }, { size: 'US 6' }, { size: 'US 7' }, { size: 'US 8' }, { size: 'US 9' }, { size: 'US 10' }, { size: 'US 11' }, { size: 'US 12' }, { size: 'US 13' }, { size: 'US 14' }, { size: 'US 15' }, { size: 'US 16' } ] const arr2 = [ { size: '4', cost: '170' }, { size: '6', cost: '75' }, { size: '7', cost: '75' }, { size: '8', cost: '78' }, { size: '9', cost: '80' }, { size: '10', cost: '85' }, { size: '11', cost: '73' }, { size: '12', cost: '77' }, { size: '14', cost: '100' } ] for (const arr1Item in arr1) { let arr1Size = arr1[arr1Item].size let includes = arr1Size.includes(arr2[arr1Item].size); if(includes) { newArr.push(arr2[arr1Item]) } else { newArr.push({size: '-'}) } } console.log(newArr)

El problema es que cada vez que ejecuto esto, aparece este error: TypeError: Cannot read properties of undefined (reading 'size')

Este error está ocurriendo debido a este código aquí:

arr2[arr1Elemento].tamaño

Lo que me confunde es si console.log(arr2[arr1Item]) DENTRO del bucle for, devuelve cada objeto de arr2 sin error, solo da un error cuando agrego .size .

He estado atascado en esto por un tiempo, agradecería cualquier ayuda. Gracias.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Itera sobre arr1 usando la variable de índice arr1Item , pero luego la usa para indexar en arr2 . Como arr1 tiene más elementos que arr2 , termina intentando acceder a arr2[9] === undefined e undefined no tiene un atributo de tamaño, de ahí el error TypeError: Cannot read properties of undefined (reading 'size') .

Un par de otros problemas

  1. newArr no es una const.

  2. Array.protoype.includes() se usa para ver si, por ejemplo, 4 de arr2.size coincide con arr.size como US 4 pero esto también coincidirá incorrectamente (subcadena) con US 14 .

Solucion potencial

Usando el hecho de que tanto arr1 como arr2 están ordenados por el atributo de size (normalizado), y que el conjunto de arr2.size (normalizado) es un subconjunto de arr1.size (normalizado), puede hacer la transformación en tiempo lineal ( O(arr1.length) ):

 const arr1 = [ { size: 'US 3' }, { size: 'US 4' }, { size: 'US 5' }, { size: 'US 6' }, { size: 'US 7' }, { size: 'US 8' }, { size: 'US 9' }, { size: 'US 10' }, { size: 'US 11' }, { size: 'US 12' }, { size: 'US 13' }, { size: 'US 14' }, { size: 'US 15' }, { size: 'US 16' } ] const arr2 = [ { size: '4', cost: '170' }, { size: '6', cost: '75' }, { size: '7', cost: '75' }, { size: '8', cost: '78' }, { size: '9', cost: '80' }, { size: '10', cost: '85' }, { size: '11', cost: '73' }, { size: '12', cost: '77' }, { size: '14', cost: '100' } ] let newArr = [] for(let i1 = 0, i2 = 0; i1 < arr1.length; i1++) { if(i2 < arr2.length && arr1[i1].size.substr(3) === arr2[i2].size) { newArr.push(arr2[i2]); i2++; } else { newArr.push({size: '-'}); } } console.log(newArr)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Creo que quieres usar el filtro para las coincidencias.

 let newArr = [] const arr1 = [ { size: 'US 3' }, { size: 'US 4' }, { size: 'US 5' }, { size: 'US 6' }, { size: 'US 7' }, { size: 'US 8' }, { size: 'US 9' }, { size: 'US 10' }, { size: 'US 11' }, { size: 'US 12' }, { size: 'US 13' }, { size: 'US 14' }, { size: 'US 15' }, { size: 'US 16' } ] const arr2 = [ { size: 'US 4', cost: '170' }, { size: '6', cost: '75' }, { size: '7', cost: '75' }, { size: '8', cost: '78' }, { size: '9', cost: '80' }, { size: '10', cost: '85' }, { size: '11', cost: '73' }, { size: '12', cost: '77' }, { size: '14', cost: '100' } ] arr1.forEach(a1Item => { let foundItems = arr2.filter(a2Item => a2Item.size === a1Item.size); // OR PARTIAL MATCH (arr1.size '4' would match arr2.size 'US 4') // let foundItems = arr2.filter(a2Item => a2Item.size.includes(a1Item.size)); if (foundItems.length > 0) { foundItems.forEach(fItem => { newArr.push(fItem); }); } else { // ADDED COST TO KEEP ITEMS CONSISTENT newArr.push({size: '-', cost: '-'}); } }); console.log(newArr);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda