Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't view Map object in Google Apps Script execution log

I literally cannot create a Map with keys and values in GAS.

function test() {
  var a = new Map([['foo','bar']]);
  console.log(a);
  var b = new Map();
  b.set('foo', 'bar');
  console.log(b);
}

running the function outputs:

{}
{}

The only thread I found that is relevant is Map object support in Google Apps Script

I have already enabled running in Chrome V8 runtime.

Link to my GAS project: https://script.google.com/d/1o6ZhubxJ8Xj_F3EhmzYCzrULMQX-7zqeSEc83Cqzpcx7sswng8KWclTl/edit?usp=sharing

Please. Someone. Help me. What is going on...

EDIT: THE PLOT THICKENS

I can .get() the values, and .size shows me that the Map is actually increasing in size. But .keys(), .values() and .entries() return nothing in the console. But Array.from(b.entries()) does return an array of entries. Wtf is this?

like image 507
Nathan Tew Avatar asked Jun 12 '26 14:06

Nathan Tew


1 Answers

After checking your code I was able to see that you are trying to print the keys, values, and entries methods directly by using:

console.log(b.keys());
console.log(b.values());
console.log(b.entries());

This is not possible because what you get in return from all of them is an iterator object and not an actual value, so first you need a way to show the values from that iterator, and that is why using Array.from() works correctly. So this is actually not a Google Apps Script issue but how Javascript works.

As an example you could also use something like:

console.log(b.keys().next().value);
console.log(b.entries().next().value);
console.log(b.values().next().value);

References:

  • Iterators
  • Keys
  • Entries
  • Values
like image 175
Fernando Lara Avatar answered Jun 14 '26 11:06

Fernando Lara