Search code examples
javascriptsendgrid

Expecting a string but getting an array


I want to get an array which has objects that contains string, but I am only getting the array without objects or string

const emails = [
  {
    email: "dot@gmail.com",
  },
  {
    email: "ke@gmail.com",
  },
];

const msg = {

  personalizations: emails.map((email) => ({
    to: [{ email: email.email }],
    subject: "Hello World from the Personalization API!",
  })),

};

when i console log msg I get:

{
  personalizations: [
    {
      to: [Array],
      subject: 'Hello World from the Personalization API!'
    },
    {
      to: [Array],
      subject: 'Hello World from the Personalization API!'
    }
  ],
}

I want get the values of to when I console log like this:

  "personalizations": [
    {
      "to": [
        {
          "email": "john@example.com"
        }
      ],
      "send_at": 1600188812
    },
    {
      "to": [
        {
          "email": "jane@example.com"
        }
      ],
      "send_at": 1600275471
    }
  ]

Solution

  • As from the output. It's very easy to do. I believe all you need is JSON.stringify()
    So: console.log(JSON.stringify(msg));, It should do the job.
    Though, for making it readable, you need JSON Prettier