router.get('/set-object', async (req, res) => {
let a = new Set([1, 2, 3]);
let b = new Map();
b.set('hello', 'world')
res.send({a: a, b: b});
})
but I get result:
{
"a": {},
"b": {}
}
why res.send() or res.json() Set or Map is {}
res
is sending JSON, but Set
and Map
are structures (types) that store data, so you need to convert them to JSON-compatible types before sending as a response. Use Array.from
function or spread
operator like this:
res.send({a: Array.from(a), b: Array.from(b)}); // You might need a more complex logic for mapping b
// OR
res.send({a: [...a], b: [...b]});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With