App脚本教程混乱

发布于 2024-11-29 20:23:11 字数 790 浏览 0 评论 0原文

教程位于:教程:使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

厌味 2024-12-06 20:23:11

一般而言,循环的功能是使用每个(不同)部门的一个属性填充 dataByDepartment 对象,其中每个属性将引用适用于该部门的一组数据。循环的每次迭代首先检查当前部门是否已有属性,如果没有,则创建它。如果确实需要创建新属性,它还会将部门添加到 departments 数组中。

一些背景知识:以下语句创建一个最初没有属性的对象:

var dataByDepartment = {};

要将值为“value1”的属性“key1”分配给该对象,然后您会说:

dataByDepartment.key1 = "value1";
// or
dataByDepartment["key1"] = "value1";

请注意,如果名为“key1”的属性已经存在,它将被覆盖。方括号语法允许您使用可变的属性键名称。所以你可以说:

var myKey = "key2";
dataByDepartment[myKey] = "value2";

这将创建一个属性,其名称等于 myKey 的计算结果(在本例中为“key2”),值为“value2”。

因此,回到您引用的实际代码,if 语句:

if (!dataByDepartment[rowData.department]) {

正在检查 dataByDepartment 是否已具有键名称等于 rowData.department 中的名称的属性。该语法是大致相当于 if (dataByDepartment[rowData.department] != undefined) 的快捷方式。

if 中的第一条语句:

dataByDepartment[rowData.department] = [];

使用 rowData.department 中的键名称和新空数组的值创建一个新属性。此时,如果该属性已经存在,它将被新的空数组覆盖,因此进行 if 测试。

if 中的第二条语句将部门名称添加到 departments 数组中:

departments.push(rowData.department);

最后,在 if 之后,添加 dataByDepartment 引用的数组[rowData.department] 添加了一个新元素:

dataByDepartment[rowData.department].push(rowData);

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 the departments array.

Some background: the following statement creates an object with initially no properties:

var dataByDepartment = {};

To assign a property "key1" with the value "value1" to that object you would then say:

dataByDepartment.key1 = "value1";
// or
dataByDepartment["key1"] = "value1";

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:

var myKey = "key2";
dataByDepartment[myKey] = "value2";

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:

if (!dataByDepartment[rowData.department]) {

is checking whether dataByDepartment already has a property with a key name equal to whatever is in rowData.department. The syntax is a shortcut roughly equivalent to if (dataByDepartment[rowData.department] != undefined).

The first statement in the if:

dataByDepartment[rowData.department] = [];

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 the departments array:

departments.push(rowData.department);

Finally, after the if, the array referenced by dataByDepartment[rowData.department] has a new element added to it:

dataByDepartment[rowData.department].push(rowData);
安静 2024-12-06 20:23:11

你快到了。

dataByDepartment[rowData.department]

// is the same as
var myKey = rowData.department // you can use dot or bracket notation
dataByDepartment[myKey]

如果该键存在,则它不是未定义 - 它将评估true。如果键不存在,那么它是未定义 - 它将评估false

所以if语句与此相同

if ( !dataByDepartment[myKey]) {

// which is logically the same as (note that === does not coerce type)
if ( typeof(dataByDepartment[myKey]) === 'undefined' ) {

询问您是否需要更多说明:)

You are getting there.

dataByDepartment[rowData.department]

// is the same as
var myKey = rowData.department // you can use dot or bracket notation
dataByDepartment[myKey]

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

if ( !dataByDepartment[myKey]) {

// which is logically the same as (note that === does not coerce type)
if ( typeof(dataByDepartment[myKey]) === 'undefined' ) {

Ask if you need more clarification :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文