Iterating over an object in google apps script

I thought my question would be answered by this one or this one , but not what I'm looking for.

I have an object in Google Script and want to iterate over each element:

var dict = {
    "foo": "a",
    "bar": "b"
};

for each key, element in dict{
    print the key and the element
}

      

Is it possible?

+4


source to share


1 answer


I usually do something like this:

var dict = {
    "foo": "a",
    "bar": "b"
};

function showProperties(){
  var keys = [];
  for(var k in dict) keys.push(k+':'+dict[k]);
  Logger.log("total " + keys.length + "\n" + keys.join('\n'));
}

      

will result in registration:



enter image description here

You get the logger in the script / view / Logs editor

+9


source







All Articles