_shop: { [key: string]: string[] } = { fruits: ['Apple', 'Orange'], vegetables: ['Tomato', 'Onions'] }
Can a one-liner code be used to modify the values of _shop
and return it in a specific format?
The desired output should look like the following.
{
"fruits": [
{
"name": "Apple",
"isRotten": false
},
{
"name": "Orange",
"isRotten": false
}
],
"vegetables": [
{
"name": "Tomato",
"isRotten": false
},
{
"name": "Onions",
"isRotten": false
}
]
}
This is my solution:
myShop: { [key: string]: any } = {}
Object.keys(_shop).forEach((key) =>
myShop[key] = _shop[key].map((item) => ({ name: item, isRotten: false }))
)