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

158
Vistas
¿Cómo toma el índice de un objeto en una matriz y concatena ese valor en la clave del objeto?

Tengo esta matriz:

 var arr = [ { itemCategory: "cats", itemSku: "12345", itemSubTotal: 10, itemQuantity: 2, }, { itemCategory: "dogs", itemSku: "56789", itemSubTotal: 5, itemQuantity: 1, } ]

y necesito este formato:

 var arr = [ { itemCategory1: "cats", itemSku1: "12345", itemSubTotal1: 10, itemQuantity1: 2, }, { itemCategory2: "dogs", itemSku2: "56789", itemSubTotal2: 5, itemQuantity2: 1, } ]

¿Cómo tomo el número de índice del objeto actual y lo agrego a la clave del objeto? Lo mejor que pude hacer es esto:

 var newArr=[]; for (var i = 0; i < arr.length; i++){ var obj = arr[i]; for (var key in obj){ var value = obj[key]; var j = i; j = j+=1; newArr.push(key + j + ": " + value); } }

pero solo me da una matriz plana:

 0: "itemCategory1: cats" 1: "itemSku1: 12345" 2: "itemSubTotal1: 10" 3: "itemQuantity1: 2" 4: "itemCategory2: dogs" 5: "itemSku2: 56789" 6: "itemSubTotal2: 5" 7: "itemQuantity2: 1"

Aprecio que solo lo estoy insertando en una matriz, pero no pude descubrir cómo actualizar las claves de objeto con esos números de índice.

Gracias por cualquier ayuda; muy apreciado.

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

0

Demostración de trabajo:

 // Input array let arr = [ { itemCategory: "cats", itemSku: "12345", itemSubTotal: 10, itemQuantity: 2, }, { itemCategory: "dogs", itemSku: "56789", itemSubTotal: 5, itemQuantity: 1, } ]; // Calculation to add index in each property of an object. arr.forEach((obj, index) => { Object.keys(obj).forEach((item) => { obj[item + (index + 1)] = obj[item]; delete obj[item]; }) }); // Result console.log(arr);

about 4 years ago · Juan Pablo Isaza Denunciar

0

En tu código, esta línea:

 newArr.push(key + j + ": " + value);

debería ser esto:

 newArr.push({[key + j]: value});

Pensé que sería más limpio asignar las claves a un nuevo objeto

 let arr = [{ itemCategory: "cats", itemSku: "12345", itemSubTotal: 10, itemQuantity: 2, }, { itemCategory: "dogs", itemSku: "56789", itemSubTotal: 5, itemQuantity: 1, } ] arr = arr.map((a, i) => { let n = {} Object.keys(a).forEach(k => n[k + (i + 1)] = a[k]); return n; }) console.log(arr)

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