具有不同类型数据的对象以及如何存储和访问它们? [英] Objects with different types of data and how to store and access them?

查看:76
本文介绍了具有不同类型数据的对象以及如何存储和访问它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前使用过MATLAB,并且存储和访问多维矩阵或数组非常容易.但是javascript/AppsScript让我头疼.我知道,使用GAS,必须使用for循环伪造2D数组并将数组放入数组中.

I've worked with MATLAB before and storing and accessing multi-dimensional matrices or arrays were a breeze. But javascript/AppsScript is giving me a headache. I know that with GAS, one has to fake a 2D array with for loops and putting arrays in arrays.

一次, array [i] [j] 的形式可以正常工作.还有一次我根本无法访问它.

At one time the form of array[i][j] works fine. Another time I can't access it at all.

我想我需要快速了解GAS中数组的工作原理.尤其是多维数组...

I guess I need a quick summary of how arrays work in GAS. Especially multi-dimension array...

话虽如此,这是为什么我需要这个答案的具体情况.

With that said, here is the specific scenario of why I need this answered.

我有类似这样的东西:(故意在第二行中保留了一个数据.这些行的长度不一定相同.)

I have something like this: (Purposefully left the second row with one data off. The rows do not necessarily have the same length.)

 A   B   C   D   E   F   G   H   I
abc 456 789 012 345 678
def 234 567 890 123 
ghi 012 345 678 901 234

第一列包含文本,其余为数字.基本上,出于这里说不清的原因,我需要两个数组:一个仅是第一列中的值,而另一个数组则是每一行中的数字数组".

The first column has text and the rest are numbers. Basically, for reasons too long to say here, I need two arrays: One that is just the values from first column and one array that has the "array" of numbers from each row.

类似这样的东西:

firstArray  = [abc, def, ghi];
secondArray = [[456, 789, 012, 345, 678], [234, 567, 890, 123], [012, 345, 678, 901, 234]];

然后访问它们,我有一个双重的 for循环设置:

Then to access them I have a double for loop setup:

for (var ii = 0; ii < firstArray.length; ii++) {
  do something with firstArray[ii];
  for (var jj = 0; jj < secondArray[ii].length) {
    do something with secondArray[ii][jj];
  }
}

要创建第二个数组,我正在使用 getValues()方法.我做这样的事情:

To make the second array, I'm using the getValues()method. I do something like this:

sh.getRange('B1:1').getValues(); 

由于我似乎找不到 RANGE lastColumn()方法,因此我必须执行"B1:1".仅针对 SHEET .使用 getValues()时,这会导致为 secondArray 捕获空白值.

I have to do 'B1:1' since I can't seem to find a lastColumn() method for a RANGE. It is only there for SHEET. This leads to grabbing blank values for the secondArray when using the getValues().

无论如何,我希望我不要太在意.任何反馈表示赞赏.我知道这篇文章的结构怪异.

Anyways, I hope I didn't blab on too much. Any feedback is appreciated. I know this post is structured weirdly.

推荐答案

我希望我在这里回答您的问题.我相信您想在电子表格中做到这一点:

I hope I'm answering you're question here. I believe you want to go from this in a spreadsheet:

 A   B   C   D   E   F   G   H   I
abc 456 789 012 345 678
def 234 567 890 123 
ghi 012 345 678 901 234

在Java数组中

为此:

to this in Javascript arrays:

firstArray  = [abc, def, ghi];
secondArray = [[456, 789, 012, 345, 678], [234, 567, 890, 123], [012, 345, 678, 901, 234]];

最佳做法是,尽可能少地使用Google Apps脚本电子表格API方法.为此,我将通过一个 getDataRange()调用获取所有数据:

It is best practice to use as few Google Apps Script spreadsheet API methods as possible. To that end I would get all the data with a single getDataRange() call:

var data = sh.getDataRange().getValues();

如前所述, getValues()将始终返回一个数组数组,其中外部数组代表行,内部数组是每一行中的单元格.因此,要提取到您的新数组中:

As you mentioned, getValues() will always return an array of arrays, where the outer array represents rows and the inner arrays are the cells in each row. So to extract into your new arrays:

var firstArray = [], secondArray = [];
for (var i = 0, length = data.length; i < length; i++) {
  firstArray.push(data[i][0]);
  secondArray[i] = [];
  for (var j = 1, width = data[0].length; j < width; j++) {
    if (!data[i][j]) break; //assumes no blank cells *within* the data
    secondArray[i].push(data[i][j]);
  }
}

请注意, map Javascript方法可能会提供一种更简洁的方法,但是我将留给知识渊博的人.

Note the map Javascript method would probably provide a more concise way of doing this, but I'll leave that to someone more knowledgeable.

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array

这篇关于具有不同类型数据的对象以及如何存储和访问它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆