2017-04-03 15 views
0

私は、複雑な(または私が複雑なものと考える)機能を持っており、Googleスクリプトで作成しようとしているキーボードに対して頭を叩いています。オブジェクトをループし、別のオブジェクトの値を一致させる

私は3つの異なるオブジェクトを管理しています。 1)sourceData 2)INDEXDATA 3)出力データ

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"}...] 

私は現在、出力データオブジェクト内のデータを作成するforループを使用して、ソースデータを反復処理しています。このループで

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

私は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; 
 
}

その目的はINDEXDATAをループであり、ループの初期時の出力データシートにデータ、ソースデータと出力で重要なパラメータと一致します。

しかし、animalIdのために取得しているのはすべて定義されていません。

私はこの問題を考え直していました。私はそれから離れて歩いていたと私は確かにどのように簡単な解決策がある実現:私はあなたがsourceDataindexDataからoutputDataを作りたいということを理解

function search(array, key, compareKey, valueKey){ 
 
    
 
    for (var i=0; i < array.length; i++) { 
 
     if (array[i][compareKey] === key) { 
 
      return array[i][valueKey]; 
 
     } 
 
    } 
 
}

+0

Googleスクリプトで作業する場合は、常にjavascriptで解決策を探してください。GoogleスクリプトはJavaScript機能をサポートしています。あなたの場合は、[ここ](http://stackoverflow.com/questions/1168807/how-can-i-add-a-key-value-pair-to-a-javascript-object)を開始したいかもしれません –

+0

あなたはどこですか?コードとは何ですか? –

答えて

0

。あなたの質問を誤解した場合、私は残念です。

次のサンプルはどうですか?

サンプル:

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

結果:スクリプトについて

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

:スクリプトで

  1. nameがキーとして使用されます。しかし、sourceDataindexDataにはキーがありません。したがって、idtypeは、outputDataのnullになります。

  2. indexindexMatch()は1次元配列である。従ってindex[j][0]は常にundefinedです。

  3. countは使用されません。

関連する問題