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

102
Vistas
how to make specific properties of object as array of object in javascript and add it to same object

I need to transform the below array of objects into a modified array of object

obj = [{
    org_id: 'CLTINTGBK0001',  
    rate: 8,
    qty: 500,
    total_rate: 35000
  },
  {
    org_id: 'CLTINTGBK0001',  
    rate: 7,
    qty: 800,
    total_rate: 38000
  }
]

Goal:

  obj =  [  {
        org_id: 'CLTINTGBK0001',  
       "qty": [
            { 
                "qty": 500,
                "rate":8
            }
        ],
        total_rate: 38000
      },
      {
        org_id: 'CLTINTGBK0001',  
       "qty": [
            { 
                "qty": 800,
                "rate":7
            }
        ],
        total_rate: 38000
      }
    ]

I have the below code however it's not working. I am trying to loop the array of obect and created different array and object and push object to array and add the array to each object of array.

let obj1 = {}
let qrty
let qty = []
for (let i = 0; i < arr.length; i++) {
    qrt1 = arr[i].qty
    rate1 = arr[i].rate
    console.log(qrty)
    obj1.qty = qrt1;
    obj1.rate = rate1;

    qty.push(obj1);
    arr[i].qty = qty
    console.log('arr', arr)
}
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can do this using Array#map and Destructuring Assignment:

const obj = [
  { org_id: 'CLTINTGBK0001', rate: 8, qty: 500, total_rate: 35000 },
  { org_id: 'CLTINTGBK0001', rate: 7, qty: 800, total_rate: 38000 },
];

let res = obj.map(({ qty, rate, ...rest }) => ({
  ...rest,
  qty: [{ qty, rate }],
}));

console.log(res);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can do:

const obj = [{
org_id: 'CLTINTGBK0001',  
rate: 8,
qty: 500,
total_rate: 35000
  },
  {
org_id: 'CLTINTGBK0001',  
rate: 7,
qty: 800,
total_rate: 38000
  }
]

var newObject = []

obj.forEach(element => {

var newElem = {}
for (const [key, value] of Object.entries(element)){
    if(key == 'qty'){
        newElem[key]= {
            qty: element['qty'],
            rate: element['rate'],
        }
    }else if (key == 'rate'){
        continue
    }else{
        newElem[key] = value
    }
}

newObject.push(newElem)
});

console.log(newObject)

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can do it like this:

let  obj = [
  { org_id: 'CLTINTGBK0001', rate: 8, qty: 500, total_rate: 35000 },
  { org_id: 'CLTINTGBK0001', rate: 7, qty: 800, total_rate: 38000 }
];

obj = obj.map((item)=> {
  return {
    org_id: item.org_id,
    total_rate: item.total_rate,
    qty:[   {   qty: item.qty, rate:item.rate } ]
  }
});

console.log(obj);

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