0

I have a multidimensional array created by taking data from a google spreadsheet. I am attempting to seperate out the data based on results in a specific column. For example:

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

Given that we want to seperate by column 2. We should get in theory:

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

Two entries being a house, and 1 being an apartment. I am haveing a huge issue with treating each entry as its own obj. Assuming it is even possible. I guess we would access specific parts of each object like so, new[0][1][1]? This obviously shouldnt work like this. Is there another way to seperate the entries in the way I am attempting? As it stands right now, I believe my code is just creating the same number of rows as in the original data.

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

QUESTION:

How can I seperate my data as multiple objects in a row of an array and be able to access specific components of each entry as shown in my example above? If this is not exactly possible in this way, is there another solution to this issue?

PaviniMan
  • 41
  • 2
  • Possible duplicate of [Multi-dimensional associative arrays in JavaScript](https://stackoverflow.com/questions/4329092/multi-dimensional-associative-arrays-in-javascript) – TidyDev Oct 23 '17 at 19:46

1 Answers1

0

First of all, your j and new (which by the way is not a valid var name) need a key if they are objects or to be used as array, like below:

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

That said and fixed, you can iterate over your array of arrays and use the column you want to use as filter to get the unique values to be used to group the final result.

Here is the final result:

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

I hope this helps you.

Good luck.