0

I have a complex (or what I consider complex) function that I'm banging my head against the keyboard trying to create in google scripts.

I am managing 3 different objects. 1) sourceData 2) indexData 3) outputData

sourceData[29] | Array | [{type:"cat", count:"3"}, {type:"dog", count:"5"}...]

indexData[29] | Array | [{type:"cat", id:"301"}, {type:"dog", id:"302"}...]

outputData[29] | Array | [{type:"cat", id:"301", count:"3"}, {type:"dog", id:"302", count:"5"}...]

I am currently iterating through the source data using a for loop to create data in the outputData object.

for (var i = 0; i < sourceData.length; i++) {
    var animals = {}; 
    outputData.push(animals);
    animal.type = sourceData[i].type;
    animal.id = lib.indexMatch(indexData, sourceData[i].type);
    animal.count = sourceData[i].count;
}

in this loop I call another function from my library called indexMatch.

function indexMatch(data, lookup) {
  var index = data; // object that contains the data to iterate through.
  for (var j = 0; j < index.length; j++){ // iterate through all rows in data
    if(index[j][0]=lookup){
      var value = index[j][1];
    }
  }
  Logger.log(j);
  return value;
}

Its purpose is to loop through the indexData and match the key parameter in the source data and output that data into the outputData sheet during the initial for loop.

However, all I am getting for animalId is undefined.

I was way overthinking this issue. I had to walk away from it and I certainly realised how simple the solution is:

function search(array, key, compareKey, valueKey){
    
    for (var i=0; i < array.length; i++) {
        if (array[i][compareKey] === key) {
            return array[i][valueKey];
        }
    }
 }
WC123
  • 97
  • 2
  • 6
  • When working with google scripts, always google for solutions in javascript. Google script support javascript function, most of them that is. For you case you might wanna start [here](http://stackoverflow.com/questions/1168807/how-can-i-add-a-key-value-pair-to-a-javascript-object) – Jack Brown Apr 04 '17 at 00:10
  • Where's your code and what is the problem? – Sangbok Lee Apr 04 '17 at 04:56

1 Answers1

0

I understand that you want to make outputData from sourceData and indexData. If I misunderstand your question, I'm sorry.

How about following sample?

Sample :

function myFunction() {
  var sourceData = [{type:"cat", count:"3"}, {type:"dog", count:"5"}];
  var indexData = [{type:"cat", id:"301"}, {type:"dog", id:"302"}];
  var outputData = [];

  sourceData.forEach(function(e){
    indexData.forEach(function(f){
      if (e.type == f.type) {
        var dic = {
          type: f.type,
          id: f.id,
          count: e.count
        };
        outputData.push(dic);      
      }
    });
  });
}

Result :

>>> [{count=3, id=301, type=cat}, {count=5, id=302, type=dog}]

About your script :

  1. In your script, name is used as a key. But sourceData and indexData don't have the key. So id and type become null in outputData.

  2. index in indexMatch() is one dimensional array. So index[j][0] is always undefined.

  3. A key count isn't used.

Tanaike
  • 181,128
  • 11
  • 97
  • 165