When I run this code I have a different result in the browser than in node
const users = [
{
id: 1,
name: "ada",
surname: "wong",
games: [
{ id: 1, name: "g1" },
{ id: 2, name: "g2" },
],
},
{
id: 2,
name: "leon",
surname: "kennedy",
games: [{ id: 2, name: "g2" }],
},
];
const output = users.map((user) => ({ ...user }));
console.log(output);
In the browser I can see the expected result
[
{
"id": 1,
"name": "ada",
"surname": "wong",
"games": [
{
"id": 1,
"name": "g1"
},
{
"id": 2,
"name": "g2"
}
]
},
{
"id": 2,
"name": "leon",
"surname": "kennedy",
"games": [
{
"id": 2,
"name": "g2"
}
]
}
]
But when I run it with node I get this result
$node app.js
ouput
[
{
id: 1,
name: 'ada',
surname: 'wong',
games: [ [Object], [Object] ]
},
{ id: 2, name: 'leon', surname: 'kennedy', games: [ [Object] ] }
]
I have tried to correct this behavior [ [Object], [Object] ]
in node in the following ways, but after trying them all return the same result
const output = users.map((user) => ({ ...user, games: user.games }));
const output = users.map((user) => ({ ...user, games: [...user.games] }));
const output = users.map((user) => ({
...user,
games: user.games.map(({ id, name }) => ({ id, name })),
}));
Why does destructuring objects within another object have a different output in the browser than in node? How to fix it in node?