Search code examples
google-apps-scriptgoogle-apps-script-editor

Why are my objects being logged with equals signs rather than semicolons / object literal notation?


I'm having the most difficult time figuring out how to get my constructor to create an array of my draft email ids / subjects in object literal notation.

Specifically, I've created my constructor that puts the subject and id of each email draft as an object into an array:

function draft(id, subject) {
  this.emailId = id;
  this.emailSubject = subject;
}

Then, when I use this constructor to put a few emails into the array (the code below is using data I manually set as a test)....

function seeDraftMessages() {
  var testArray = [];
  testArray.push(new draft(12412, 'test subject 1'));
  testArray.push(new draft(75162, 'test subject 2'));
  var testArrayDetails = [];
  testArray.forEach(function(msg) {
    Logger.log(msg);
  });
};

I get this as the output in Logs:

[19-05-22 14:35:06:863 CDT] {emailId=12412.0, emailSubject=test subject 1}
[19-05-22 14:35:06:864 CDT] {emailId=75162.0, emailSubject=test subject 2}

The problem is that these objects are not in object literal notation, so it's hard for me to work with them in my HTML interface. I need them to be formatted like this, instead:

[19-05-22 14:35:06:863 CDT] {emailId: '12412', emailSubject: 'test subject 1'}
[19-05-22 14:35:06:864 CDT] {emailId: '75162', emailSubject: 'test subject 2'}

Any help would be greatly appreciated!

Davis


Solution

  • Logger.log transforms them into = for display purposes. The underlying data is still a valid JavaScript object.

    Try

    Logger.log(JSON.stringify(msg));