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)
}
total_rateshould be 38000? Or the first one should be 35000?qtyarray why have the array at all. Why not justqty: { amount: 5, rate: 3 }?