I have this problem and I can't figure why. inside a for loop I try to assign a new key value pair to each object in an array of objects, response from a mongoose query. e.g.
obj = {
value1: "someValue",
value2: [],
value3: {}
value4: {
id: "someId"
}
}
if I try to do a obj.value4.newKey = "newValue" nothing seems to happen, but the thing is that when I do
console.log(obj.value4.newKey) // prints "newValue"
but when I do
console.log(obj)
or
console.log(obj.value4)
the new key added previously doesn't seems to exists
I really don't know why, but I've found some kind of workaround, this it what worked (continuing with the first example)
res = JSON.parse(JSON.stringify(obj.value4));
res.newKey = "newValue";
now I do
console.log(res)
and I get
{
id: "someId",
newKey = "newValue"
}
any ideas ?
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