创建数组对象的多维数组

时间:2017-10-23 19:29:10

标签: javascript google-spreadsheet-api

我有一个通过从谷歌电子表格中获取数据而创建的多维数组。我试图根据特定列中的结果分离数据。例如:

    var j = {
      ["Mine", "House"],
      ["Your", "House"],
      ["his", "apt"]
    }

鉴于我们想要按第2列分开。我们应该理论上:

var new = {
          [["Mine", "House"] , ["Your", "House"]],
          [["his", "apt"]]
        }

两个条目是一个房子,一个是公寓。将每个条目视为自己的对象,我遇到了一个巨大的问题。假设它甚至可能。我想我们会像new[0][1][1]那样访问每个对象的特定部分? 这显然不应该像这样工作。是否有另一种方式以我尝试的方式分隔条目?就目前而言,我相信我的代码只是创建与原始数据相同的行数。

 var spreadsheet = SpreadsheetApp.getActive();
      spreadsheet.insertSheet("Report");

      var data_sheet =spreadsheet.getSheetByName("Data");
      var genCounsel_data = data_sheet.getRange(240, 1, 96, 7).getValues(); //get genCounseling data
      var report_sheet = spreadsheet.getSheetByName("Report");

      //setup key values for columns in report sheet
      report_sheet.appendRow(["Student Service", "Unit Student Service Outcome", "Indicators Data Sources", "How indicator was measured", "Benchmark Data", "Assigned to do Assessment", "Email"])

      //seperate out by SS outcomes
      var genCounselDataByOutcomes = new Array(new Array()); //all responses for each outcome, also parrellel
      var outcomes_freq = new Array(); //parrellel arrays
      var found = false;

      //get services and frequency;
      for(var i=0; i<genCounsel_data.length; ++i){
        genCounsel_data[i][OUTCOMES_COL].toString().toLowerCase();
        for(var j=0; j<genCounselDataByOutcomes.length && !found; ++j){
          if(genCounselDataByOutcomes[j][OUTCOMES_COL] == genCounsel_data[i][OUTCOMES_COL]){
            genCounselDataByOutcomes[j].push(genCounsel_data[i]); //ADD to row with same outcomes
            ++outcomes_freq[j]; //update amount of entries in said outcome
            found = true;
          }
        }
        if(found == false){
          genCounselDataByOutcomes.push(new Array);
          genCounselDataByOutcomes[genCounselDataByOutcomes.length-1].push([genCounsel_data[i]]);
          outcomes_freq.push(1);
        }
        else
          found = false;
      }

      for(var i=0; i<outcomes_freq.length;++i)
        Logger.log(outcomes_freq[i]);

      //for each outcome select a random one and move entire row to sheet;
      for(var i=0; i<genCounselDataByOutcomes.length; ++i){
        Logger.log(genCounselDataByOutcomes[i]);
      }

问题:

如何将数据作为数组中的多个对象分离,并且能够访问每个条目的特定组件,如上例所示?如果以这种方式不可能实现这一点,那么这个问题还有另一种解决方案吗?

1 个答案:

答案 0 :(得分:0)

首先,您的jnew(顺便说一下,它不是有效的var名称)如果是对象或者用作数组,则需要一个键,如下所示:

var j = [
    ["Mine", "House"],
    ["Your", "House"],
    ["his", "apt"]
];
var newVar = [
    [["Mine", "House"] , ["Your", "House"]],
    [["his", "apt"]]
];

说了并修复了,您可以遍历数组数组并使用要用作过滤器的列来获取用于对最终结果进行分组的唯一值。

以下是最终结果:

&#13;
&#13;
var j = [
        ["Mine", "House"],
        ["Your", "House"],
        ["his", "apt"]
    ];
    
var uniqueValues = [];
var indexColumn = 1; //Here you specify the column you want to use to filter/group the elements from j
j.forEach(function(element){
    if(!uniqueValues.includes(element[indexColumn])){
        uniqueValues.push(element[indexColumn]);
    }
});

//Now you use the `uniqueValues` to filter the `j` array and generate the `groupedArrays` array, which is the one you are looking for:
var groupedArrays = [];
uniqueValues.forEach(function(uniqueValue){
    groupedArrays.push(j.filter(function(element){
        return element[indexColumn] === uniqueValue;
    }));
});

console.log(groupedArrays);
&#13;
&#13;
&#13;

我希望这会对你有所帮助。

祝你好运。