I have an array of objects like this:
const input = [
{
"name" : "car",
"sign" : "+",
"options" : "benz"
},
{
"name" : "bike",
"sign" : "+",
"options" : "pulsar"
},
{
"name" : "bike",
"sign" : "+",
"options" : "enfield"
},
{
"name" : "car",
"sign" : "+",
"options" : ["toyota","hyundai","benz"]
},
{
"name" : "",
"sign" : "",
"options" : ""
},
{
"name" : "car",
"sign" : "+",
"options" : ["audi", "ford"]
}
]
The input structure is like this: every object has 3 key-value pairs.
All of them can be empty
It can have valid values:
a. The options can either be a string
b. Or it can be an array
I dont want the objects with values of "" (empty string) to be added in the output, The output should be like this:
[
{"car" : [ "benz", "toyota","hyundai", "audi", "ford"]},
{"bike" : ["pulsar","enfield" ]}
]
The various other answers do not show how to handle when the object value can be either an array or a string, So I'm writing a new question.
I am not sure if I have to use reduce or map. Any help is greatly appreciated, Thanks in advance.