javascript 迭代 Google Apps 脚本中的对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25722682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 04:54:05  来源:igfitidea点击:

Iterate over an object in Google Apps script

javascriptgoogle-apps-script

提问by LondonRob

I thought my question would be answered with thisor thisbut neither is what I'm looking for.

我以为我的问题会用这个这个来回答,但都不是我要找的。

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

我在 Google Script 中有一个对象,想遍历每个元素:

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

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

Is this possible?

这可能吗?

回答by Serge insas

I usually do something like that :

我通常做这样的事情:

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'));
}

result in Logger :

结果记录器:

enter image description here

在此处输入图片说明

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

您在脚本编辑器/视图/日志中获得记录