2

I have the following map:

  let mapObject = new Map();
  mapObject.set("one", "file1");
  mapObject.set("two", "file2");
  mapObject.set("three", "file3");

  console.log([...mapObject.entries()]);

and the console generates the following output:

[ [ 'one', 'file1' ], [ 'two', 'file2' ], [ 'three', 'file3' ] ]

I took this idea from this post, but this solution doesn't work if you add a string in the console statement:

console.log("This is a map: " + [...mapObject.entries()]);

it generates the following output instead:

This is a map: one,file1,two,file2,three,file3

which is less friendly to read. I would like in some how to have the same output I would obtain from: console.log([...mapObject.entries()]) but to be able to store it in a string for a general purpose. Is there a simple way to achieve it that works for google apps script?

David Leal
  • 6,373
  • 4
  • 29
  • 56

1 Answers1

2

You can use JSON.stringify:

let mapObject = new Map();
mapObject.set("one", "file1");
mapObject.set("two", "file2");
mapObject.set("three", "file3");

const res = JSON.stringify([...mapObject.entries()])

console.log("This is a map: " + res);
Spectric
  • 30,714
  • 6
  • 20
  • 43
  • 1
    it works thanks!, I though for this solution I would need to install some JSON library, but it doesn't require any additional setup. – David Leal Nov 06 '21 at 18:48