i was trying to add a nested object in my javascript object. i coundn't find any solution over the web. i have a response which is coming from the server and i need to set some values before storing the same data into the db.
here's the object :
"currentMembership": {
                    "messages": {
                        "isUnlimited": false,
                        "count": 5
                    },
                    "likes": {
                        "isUnlimited": false,
                        "count": 5
                    },
                    "matches": {
                        "isUnlimited": false,
                        "count": 0,
                        "validFor": 0
                    },
                    "backtrack": {
                        "isUnlimited": false,
                        "count": 0
                    }
                }
and i am trying to add a dailyCount object to some of my objects such that the object in return is updated as:
"currentMembership": {
                    "messages": {
                        "dailyCount": {
                            "date": "2019-12-10T19:33:22.793Z"
                            "count": 5
                        },
                        "isUnlimited": false,
                        "count": 5
                    },
                    "likes": {
                        "dailyCount": {
                            "date": "2019-12-10T19:33:16.664Z",
                            "count": 5
                        },
                        "isUnlimited": false,
                        "count": 5
                    },
                    "matches": {
                        "dailyCount": {
                            "date": "2019-12-10T19:33:16.697Z",
                            "count": 0
                        },
                        "isUnlimited": false,
                        "count": 0,
                        "validFor": 0
                    },
                    "backtrack": {
                        "dailyCount": {
                            "date": "2019-12-10T19:33:16.701Z",
                            "count": 0
                        },
                        "isUnlimited": false,
                        "count": 0
                    },
                    "hasPremiumPlan": false,
                    "hasDMPlan": false,
                    "filters": false
                }
any help would be great! :)
You can use for...in to loop over an object's properties.
var i;
for (i in currentMembership) {
  currentMembership[i].dailyCount = {
    date: "2019-12-10T19:33:16.701Z",
    count: 0
  }
}
If you're only targeting certain properties, create an array with those keys and loop over those.
for (var keys = ['messages', 'likes', 'matches', 'backtrack'], i = 0, j = keys.length; i < j; i++) {
  currentMembership[keys[i]].dailyCount = {
    date: "2019-12-10T19:33:16.701Z",
    count: 0
  }
}
                        okay so i was trying to change the object which was returned as a mongoose object and therefore i could not change it, but after converting into a javascript object.
currentMembership = currentMembership.toObject();
This way i could edit the object.
i could also use lean() function with find query.
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