-1

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
    },
    ...
]
0

3 Answers 3

1

Here you go:

const data = {
    item001: '200',
    item002: '350',
}

const arrData = Object.keys(data).map(product => ({
    product: product,
    price: Number(data[product])
}))
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! That's exactly what I was after.
0

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)

Comments

-1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.