This is my case.
data: [
{
q: "question 1",
a: "answer 1"
},
{
q: "question 2"
}
]
How can I map this into key: value
pair so that the final output is { "question 1": "answer 1"}
?
The trick is that only if a
property exists then it should be assigned to the new object as above example { "question N": "answer N"}
.
I have tried combining .map()
and .filter()
, but it didn't work.
For example:
const obj = data.map(e => e.q).filter(s => s.a)
Thanks.