I have a problem with node.js to object to json string
var chat = {};
chat.messages = [];
chat.messages['en'] = [];
chat.messages['fr'] = [];
console.log(chat.messages)
console.log(JSON.stringify(chat.messages));
I got
[ en: [], fr: [] ]
[]
I don’t know why this is not correctly convert
On this line, you initialize
chat.messagesas an empty array:Here, you use it as an object:
These lines actually set properties on the array instance. It’s curious that Node would include these properties in the normal
.toString()result (i.e., that you’d see the set properties as elements of the array onconsole.log(chat.messages).In any case, to fix, declare
chat.messagesas an object: