I need to convert a hash map
{
"fruit" : ["mango","orange"],
"veg" : ["carrot"]
}
to
[
{ "type" : "fruit" , "name" : ["mango","orange"] } ,
{ "type" : "veg" , "name" : ["carrot"] }
]
how do I do that??
I need to convert a hash map
{
"fruit" : ["mango","orange"],
"veg" : ["carrot"]
}
to
[
{ "type" : "fruit" , "name" : ["mango","orange"] } ,
{ "type" : "veg" , "name" : ["carrot"] }
]
how do I do that??
In case of using underscore.js:
Not exactly the answer you are looking for, but it could be useful for general purpose.
It looks simple, key of your map is type and values are name, so just loop thru map and insert object in a list e.g.
output:
For those using ES6 maps...
Assuming you have...
You can use...
Note that Array.from takes iterables as well as array-like objects.
I would like to give an "oneline" solution:
Economy of words at your service. Question asked for translating an object to an array, so I'm not duplicating above answer, isn't it?
No Need of loop