7

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.

1
  • What should happen if the cc field already exists? Commented Jul 5, 2017 at 12:06

2 Answers 2

9

Put strings inside []:

replyDataObj['payload']['emailAddresses']['cc']={
     "address": [
       ccEmailId
      ]
  } 
Sign up to request clarification or add additional context in comments.

1 Comment

Corrected it, added strings and my issue is solved. Thank you
1

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
  ]
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.