Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add a nested object in a current existing javascipt object

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! :)

like image 663
Ashvini Panwar Avatar asked Dec 10 '19 19:12

Ashvini Panwar


2 Answers

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
  }
}
like image 66
SethC Avatar answered Oct 25 '22 09:10

SethC


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.

like image 27
Ashvini Panwar Avatar answered Oct 25 '22 08:10

Ashvini Panwar