Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

44
Vistas
How do you take the index of an object in an array and concatenate that value onto the object key?

I have this array:

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

and I need this format:

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

How do I take the index number of the current object and append it to the key of the object? The best I could do is this:

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);     
     }
}

but it only gives me a flat array:

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

Appreciate I'm only pushing it into an array, but I could not figure out how to update the object keys with those index numbers.

Thanks for any help; much appreciated.

7 months ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Working Demo :

// 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);

7 months ago · Juan Pablo Isaza Denunciar

0

In your code, this line:

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

should be this:

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

I thought it might be cleaner to map the keys over to a new object

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)

7 months 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 empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.