I have some code that outputs an object with key value pairs. Like so:
{"a" : "1", "b" : "2", "c" : "3"}
I would like to change that output to this instead:
[{"a" : "1"}, {"b" : "2"}, {"c" : "3"}]
I have fiddled around with Object.entries(), Object.assign() and Array.map() without getting much further. I have put so much time into this now so I figured it was time to ask for some help.
The initial data is from req.query which I have then managed to convert from:
{a: "1", b: "2", c: "3"}
into
{"x.a": "1", "x.b": "2", "x.c": "3"}
This will eventually become a mongoDB query.
Below is what I have so far.
let obj = Object.entries(req.query);
obj = obj.map(([key, val]) => ["x."+ key, val]);
let newobj = Object.assign(...obj.map(([k, v]) => ({ [k]: v })));
which gives:
{"x.a": "1", "x.b": "2", "x.c": "3"}
so, as per my initial question. How do I turn this into:
[{"x.a" : "1"}, {"x.b" : "2"}, {"x.c" : "3"}]