Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add key values dynamically to nested object in Javascript

This might be a duplicate of this but did not get proper solution over there. I have object as below,

var replyDataObj = {

                  "department": {
                    "name": getCache("departmentName")
                  },
                  "subject": replyEmailSubject,  
                  "payload": {
                    "email": {
                      "contents": {
                        "content": [
                          {
                            "type": "html",
                            "value": replyEmailContent
                          }   
                        ]
                      },
                      "emailAddresses": {
                        "from": fromEmailId,
                        "to": {
                          "address": [
                            toEmailId
                          ]
                        }        
                      }
                    }
                  }  
            }

I want to add following key values to 'emailAddresses' key dynamically depending upon whether cc field is present or not,

"cc": {
         "address": [
           ccEmailId
          ]
      }  

So it will look like,

var replyDataObj = {

              "department": {
                "name": getCache("departmentName")
              },
              "subject": replyEmailSubject,  
              "payload": {
                "email": {
                  "contents": {
                    "content": [
                      {
                        "type": "html",
                        "value": replyEmailContent
                      }   
                    ]
                  },
                  "emailAddresses": {
                    "from": fromEmailId,
                    "to": {
                      "address": [
                        toEmailId
                      ],
                    "cc": {
                      "address": [
                         ccEmailId
                        ]
                      } 
                    }        
                  }
                }
              }  
        }

I tried to add this using object[key] as below, object.key but no luck

replyDataObj[payload][emailAddresses][cc]={
     "address": [
       ccEmailId
      ]
  } 

I tried multiple ways and searched a lot but did not get the solution. Any help in this regard will be greatly appreciated. Thank you.

like image 725
Vikas Yadav Avatar asked Jul 05 '17 12:07

Vikas Yadav


2 Answers

Put strings inside []:

replyDataObj['payload']['emailAddresses']['cc']={
     "address": [
       ccEmailId
      ]
  } 
like image 85
K. Kirsz Avatar answered Oct 13 '22 00:10

K. Kirsz


As answered by @K.Kirsz, I was missing strings inside [] (quotes). Also added missing 'email' key to solve my issue.

replyDataObj['payload']['email']['emailAddresses']['cc']={
 "address": [
   ccEmailId
  ]
} 
like image 26
Vikas Yadav Avatar answered Oct 12 '22 23:10

Vikas Yadav