How do you go from a single object like this:
{
item001: '200',
item002: '350',
...
}
to an array of objects with named properties like this:
[
{
product: 'item001',
price: 200
},
{
product: 'item002',
price: 350
},
...
]
Here you go:
const data = {
item001: '200',
item002: '350',
}
const arrData = Object.keys(data).map(product => ({
product: product,
price: Number(data[product])
}) )
var main = {
item001: '200',
item002: '350'
}
function doThis(items){
var keys = Object.keys(items);
var obj = keys.map(key=>({product:key,price:items[key]}))
return obj;
}
console.log(doThis(main))
You can use entries and map methods to iterate over and map key/value pairs of an object:
const items = { item001: '200', item002: '350' };
const new_items = Object.entries(items).map((item)=>({ product:item[0], price:item[1]}));
console.log(new_items)