0

In all the examples of new Set it shows how to convert array to set, add to set, etc and then shows what to expect when using console.log . Is there a difference with Google Apps Script? Seems so simple, not sure what Im missing?

function myFunction() {
  let arr = ["one", "two", "three"]
  let setTest = new Set(arr)
  Logger.log(setTest)
  // Output: 
//Info  {}
}

The following example shows how to create a new Set from an array.

let chars = new Set(['a', 'a', 'b', 'c', 'c']);  

All elements in the set must be unique therefore the chars only contains 3 distinct elements a, b and c.

console.log(chars);

Output:

Set { 'a', 'b', 'c' }
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
  • what if you do `Logger.log(JSON.stringify(setTest))` ? maybe it's the toString version of the Set which has a problem – ValLeNain Oct 27 '22 at 15:24
  • 1
    If you want to print a set, you need to spread the values into an array and convert it to JSON. This might help: https://stackoverflow.com/a/73989963/1762224 – Mr. Polywhirl Oct 27 '22 at 15:26
  • also, you have the `size` attribute that you can print on a Set to see how many elements are in there. – ValLeNain Oct 27 '22 at 15:32
  • I don't understand your question `setTest` is a Set object. What do you want to do with `setTest`? – TheWizEd Oct 27 '22 at 15:49
  • @ValLeNain .size prints 3 – Mr. Anderson Oct 27 '22 at 15:59
  • @Mr.Polywhirl from Array is how I have been viewing, Im just curious why alot of the examples and training show you should be able to console.log the Set. Trying to understand – Mr. Anderson Oct 27 '22 at 16:00
  • @TheWizEd Just learning about Set and arrays. From all the training and documentation it shows you can just print the Set contents. I was adding and deleting things so just as I go along I wanted to check what was it contained, etc – Mr. Anderson Oct 27 '22 at 16:02
  • Still not understanding your question. The Set object has a `toString()` method that is called by default if you want to display the object. For Chrome it will appear as an object `{ 'a', 'b', 'c' }` . See [Set Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) – TheWizEd Oct 27 '22 at 16:12
  • @TheWizEd If I try to display, I get `{}` Im trying to do the same in the link you posted as `console.log(mySet1) // logs Set(5) [ 1, "some text", {…}, {…}, 5 ] in Firefox // logs Set(5) { 1, "some text", {…}, {…}, 5 } in Chrome` – Mr. Anderson Oct 27 '22 at 16:19
  • If you are using Chrome that is the expected results. – TheWizEd Oct 27 '22 at 16:25

1 Answers1

1
function myFunction() {
  let arr = ["one", "two", "three"]
  let s = new Set(arr);
  let it = s.values;
  Logger.log([...s])
}

Execution log
11:52:44 AM Notice  Execution started
11:52:45 AM Info    [one, two, three]
11:52:46 AM Notice  Execution completed
Cooper
  • 59,616
  • 6
  • 23
  • 54