App脚本教程混乱
教程位于:教程:使用 JavaScript 对象编写电子表格数据
完整的代码可以在教程末尾找到。
我没有得到第一个函数 runExample() 中的 for 循环,
for (var i = 0; i < data.length; ++i) {
var rowData = data[i];
if (!dataByDepartment[rowData.department]) {
dataByDepartment[rowData.department] = [];
departments.push(rowData.department);
}
dataByDepartment[rowData.department].push(rowData);
}
我没有得到 if 语句内部发生的情况。 dataByDepartment[rowData.department]) 是什么意思???
dataByDepartment 最初是空的...这是在创建属性吗?
有人可以解释一下整个循环在做什么吗?非常感谢!
PS:我对 Javascript 还很陌生……来自 C 编程,我总是对对象和属性的创建感到困惑……
The tutorial is here: Tutorial: Writing Spreadsheet data using JavaScript Objects
The full code can be found at the end of the tutorial.
I don't get the for loop in the first function, runExample()
for (var i = 0; i < data.length; ++i) {
var rowData = data[i];
if (!dataByDepartment[rowData.department]) {
dataByDepartment[rowData.department] = [];
departments.push(rowData.department);
}
dataByDepartment[rowData.department].push(rowData);
}
I don't get what is going on inside the if-statement.
What do they mean by dataByDepartment[rowData.department]) ???
dataByDepartment is initially empty... Is this creating a property??
Can someone please explain what that whole loop is doing? Thank you very much!
PS: I am still quite new to Javascript... Coming from C programming I am always confused by the object and property creations...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般而言,循环的功能是使用每个(不同)部门的一个属性填充
dataByDepartment
对象,其中每个属性将引用适用于该部门的一组数据。循环的每次迭代首先检查当前部门是否已有属性,如果没有,则创建它。如果确实需要创建新属性,它还会将部门添加到departments
数组中。一些背景知识:以下语句创建一个最初没有属性的对象:
要将值为“value1”的属性“key1”分配给该对象,然后您会说:
请注意,如果名为“key1”的属性已经存在,它将被覆盖。方括号语法允许您使用可变的属性键名称。所以你可以说:
这将创建一个属性,其名称等于
myKey
的计算结果(在本例中为“key2”),值为“value2”。因此,回到您引用的实际代码,if 语句:
正在检查 dataByDepartment 是否已具有键名称等于 rowData.department 中的名称的属性。该语法是大致相当于
if (dataByDepartment[rowData.department] != undefined)
的快捷方式。if
中的第一条语句:使用
rowData.department
中的键名称和新空数组的值创建一个新属性。此时,如果该属性已经存在,它将被新的空数组覆盖,因此进行 if 测试。if
中的第二条语句将部门名称添加到departments
数组中:最后,在
if
之后,添加dataByDepartment 引用的数组[rowData.department]
添加了一个新元素:In general terms the function of the loop is to populate the
dataByDepartment
object with one property for each (distinct) department, where each property will reference an array of data applicable to that department. Each iteration of the loop first checks whether there is already a property for the current department and if not it creates it. If it does need to create a new property it also adds the department to thedepartments
array.Some background: the following statement creates an object with initially no properties:
To assign a property "key1" with the value "value1" to that object you would then say:
Note that if a property called "key1" already existed it would be overwritten. The square-bracket syntax allows you to use property key names that are variable. So you can say:
Which will create a property with a name equal to whatever
myKey
evaluates to ("key2" in this case), and the value "value2".So getting back to the actual code you quote, the if statement:
is checking whether
dataByDepartment
already has a property with a key name equal to whatever is inrowData.department
. The syntax is a shortcut roughly equivalent toif (dataByDepartment[rowData.department] != undefined)
.The first statement in the
if
:creates a new property with the key name of whatever is in
rowData.department
and the value of a new empty array. At that point if the property already existed it would be overwritten by a new empty array, hence the if test.The second statement in the
if
adds the department name to thedepartments
array:Finally, after the
if
, the array referenced bydataByDepartment[rowData.department]
has a new element added to it:你快到了。
如果该键存在,则它不是未定义 - 它将评估true。如果键不存在,那么它是未定义 - 它将评估false
所以if语句与此相同
询问您是否需要更多说明:)
You are getting there.
If the key exists then it is not undefined - it will evaluate true. If the key does not exist then it is undefined - it will evaluate false
So the if statement is the same as this
Ask if you need more clarification :)