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

101
Views
cómo hacer propiedades específicas del objeto como una matriz de objetos en javascript y agregarlo al mismo objeto

Necesito transformar la siguiente matriz de objetos en una matriz modificada de objetos

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

Meta:

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

Tengo el siguiente código, sin embargo, no funciona. Estoy tratando de hacer un bucle en la matriz de obect y creé una matriz y un objeto diferentes y empuje el objeto a la matriz y agregue la matriz a cada objeto de la matriz.

 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 answers
Answer question

0

Puede hacer esto usando Array#map y Destructuring Assignment de desestructuración:

 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 Report

0

Tu puedes hacer:

 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 Report

0

Puedes hacerlo así:

 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 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!