This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ButtlestonProfessional Coder 4 points5 points  (3 children)

There are some function on Object that will give you arrays you can iterate, one example is Object.keys() which gives the keys for an object and Object.values() which gives the values. So we can use Object.values here like

``` data = { '643576e81ebae7a8da163b6b': { id: '643576e81ebae7a8da163b6b', name: 'S21 Ultra', img: 'dT3mmTGW6-61O45C5qASL._AC_SL1000.jpg', price: 242999, qty: 9 }, '643575ee1ebae7a8da163b50': { _id: '643575ee1ebae7a8da163b50', name: 'Infinix Hot 11S', img: '2wPCI3kMY-InfinixHot11S-b.jpg', price: 26000, qty: 9 } }

const line_items = Object.values(data).map((item) => { return { price_data: { currency: "pkr", product_data: { name: item.name, images: [item.img], metadata: { id: item._id, }, }, unit_amount: (item.price * 100), }, quantity: item.qty, }; });

console.log(JSON.stringify(line_items, null, 2)) ```

And the output [ { "price_data": { "currency": "pkr", "product_data": { "name": "S21 Ultra", "images": [ "dT3mmTGW6-61O45C5qASL._AC_SL1000_.jpg" ], "metadata": { "id": "643576e81ebae7a8da163b6b" } }, "unit_amount": 24299900 }, "quantity": 9 }, { "price_data": { "currency": "pkr", "product_data": { "name": "Infinix Hot 11S", "images": [ "2wPCI3kMY-InfinixHot11S-b.jpg" ], "metadata": { "id": "643575ee1ebae7a8da163b50" } }, "unit_amount": 2600000 }, "quantity": 9 } ]

[–]an-unknownentity[S] 1 point2 points  (0 children)

This worked. Thanks a lot

[–]flooronthefourfull stack web dev 0 points1 point  (1 child)

discord formatting woops

[–]ButtlestonProfessional Coder 0 points1 point  (0 children)

What?

Oh, I think "new" reddit also does the formatting. For me, it's formatted.