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.
Put strings inside []:
replyDataObj['payload']['emailAddresses']['cc']={
     "address": [
       ccEmailId
      ]
  } 
                        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
  ]
} 
                        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