1

enter image description here

I have the following function:

  var rows = sheet2Json(sheet);

  var emails = rows.filter(function (el) { //ONLY CHECKED ROWS.
         return el != "";
    })
               .map(function (row) { //ONLY CHECKED ROWS.
      return row['EMAIL'];
    });  



  Logger.log(emails)
  return (emails);
}

rows produces the sheet in an array of objects that looks like:

[{ EMAIL=xxx,  TEMPLATE=CONSULT, Index=Thu Jan 24 16:26:02 GMT-05:00 2019 }
...
]

I want an array of all the emails filtering out the empty rows. With the code above I'm getting:

[ [email protected], [email protected],  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ]

How can I get rid of the empty rows?

2 Answers 2

2

You have to get the EMAIL property first, then filter the entries which are empty or undefined:

var rows = [
  { EMAIL: '[email protected]', prop: 'bla' },
  { EMAIL: '', prop: 'bla' },
  { EMAIL: undefined },
  { EMAIL: '[email protected]', prop: 'bla' },
  { },
  undefined
];

var emails = rows
  .map(function (row) { return row && row.EMAIL; })
  .filter(Boolean);

console.log(emails);

The reason it's not working the other way is that you need to filter out the empty EMAIL fields, but you are filtering rows by comparing them to an empty string. An empty row ({}) will not pass your filter test.

To make it work the other way, you need to filter the rows based on their EMAIL property first, then map:

var rows = [
  { EMAIL: '[email protected]', prop: 'bla' },
  { EMAIL: '', prop: 'bla' },
  { EMAIL: undefined },
  { EMAIL: '[email protected]', prop: 'bla' },
  { },
  undefined
];

var emails = rows
  .filter(function (row) { return row && row.EMAIL; })
  .map(function (row) { return row.EMAIL; });

console.log(emails);

Sign up to request clarification or add additional context in comments.

4 Comments

Apps Script is basically JS 1.6. No includes, no let, no iterators, no arrow syntax. It does have map, filter, reduce, some, and every support.
@user61629, I updated the answer and added more explanations
regarding "return row && row.EMAIL;" - I'm assuming that means if row exists return row.Email ?
@user61629, Yes you are right, if row is falsy, the right most expression is not evaulated and undefined is returned
2
  • You want to retrieve the values of EMAIL from the object like [{ EMAIL=xxx, TEMPLATE=CONSULT, Index=Thu Jan 24 16:26:02 GMT-05:00 2019 }...] using Google Apps Script.

If my understanding is correct, How about this modification? In this modification, I used reduce(). Please think of this as just one of several answers.

Modified script:

var emails = rows.reduce(function (ar, row) {
  row['EMAIL'] && ar.push(row['EMAIL']);
  return ar;
}, []);

Reference:

3 Comments

Thank you! its a great example!
regarding "row['EMAIL'] && ar.push(row['EMAIL']" - does this mean "if email exists push it to the accumulator array" ?
@user61629 Yes. That's right. Also, By putting row['EMAIL'] before ar.push(row['EMAIL']), at first, row['EMAIL'] is compared.

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.