Is there a way to improve this code to convert object to mapped array?
I have came up with this solution, is there a better approach and cleaner?
let productItems = {
'Item1': {
quantity: 4,
name: 4,
price: 123
},
'Item2': {
quantity: 1,
name: 3,
price: 144
},
'Item3': {
quantity: 2,
name: 2,
price: 343
}
}
let items = [];
for (const item in productItems) {
const formatItem = {
"Qty": productItems[item].quantity,
"Cost": productItems[item].price
}
items.push(formatItem);
}
Output:
[ { Qty: 4, Cost: 123 },
{ Qty: 1, Cost: 144 },
{ Qty: 2, Cost: 343 } ]