简体   繁体   English

Google Apps脚本在两个数组中查找具有相同列值的行

[英]Google Apps Script find rows in two arrays that have the same column value

A = [{ 123, abc}, { 456, abc}, { 111, def}]
B = [{ 123, xyz}, { 111, def}, { 891, def}]

I have stored values of a google spreadsheet table in the two arrays A & B. I now want to create a new array C that only has the rows of array A where the first column of A matches the first column of B. 我已将Google电子表格的值存储在两个数组A和B中。现在,我想创建一个新数组C,该数组仅包含数组A的行,其中A的第一列与B的第一列匹配。

Note: The first column is a unique ID. 注意:第一列是唯一ID。 There won't be any duplicates within each array's first column. 每个数组的第一列中将没有任何重复项。

In the example given this would result in: 在给出的示例中,这将导致:

c =  [{ 123, abc}, { 111, def}]

Is this possible? 这可能吗?

This code does what you want. 这段代码可以满足您的需求。 I've tested it. 我已经测试过了 You can view the LOGS, and see the results. 您可以查看日志,并查看结果。 The code creates two new objects from the arrays of objects. 该代码从对象数组创建两个新对象。

var A = [{ 123:"abc"}, { 456:"abc"}, { 111:"def"}];

Is used to create: 用于创建:

var newObjA = {123:"abc", 456:"abc", 111:"def"};

And the same for array B. Then the code loops through the newObjA, and uses the key from each element in newObjA to look for a key of the same value in newObjB. 然后,代码循环遍历newObjA,并使用newObjA中每个元素的键在newObjB中查找具有相同值的键。 If there is a match, a value is returned. 如果存在匹配项,则返回一个值。 If there is no matching key of the same value, then undefined is returned. 如果没有相同值的匹配键,则返回undefined

If undefined is returned, then there is no match, and no data get put into the final array. 如果返回undefined ,则没有匹配项,也没有数据放入最终数组中。 If there is a match, the matching element from newObjA is put into the final array. 如果存在匹配项,则将来自newObjA的匹配元素放入最终数组中。

There is a lot of data manipulation to retrieve and move data from one format into another. 有很多数据操作来检索数据并将其从一种格式移动到另一种格式。 You'll need to study the code to figure it out. 您需要研究代码才能弄清楚。

function compareData() {
  var A = [{ 123:"abc"}, { 456:"abc"}, { 111:"def"}];
  var B = [{ 123:"xyz"}, { 111:"def"}, { 891:"def"}];

  Logger.log("A: " + A);
  Logger.log("B: " + B);

  var arryLengthA = A.length;
  var arryLengthB = B.length;

  var newObjA = {};
  var newObjB = {};
  var finalArry  = [];
  var thisIterationObj = {};

  //Convert Array A to an Ojbect A
  for (var i = 0; i < arryLengthA; i++) {
    Logger.log("count: " + i);
    thisIterationObj = {}; //reset to empty
    thisIterationObj = A[i];
    Logger.log("thisIterationObj: " + thisIterationObj);

    for (var thisKey in thisIterationObj) {
      Logger.log("thisKey: " + thisKey);
      Logger.log(thisIterationObj[thisKey]);

      newObjA[thisKey] = thisIterationObj[thisKey];
      Logger.log(newObjA[thisKey]);
    }
  };

  //Convert Array B to an Ojbect B
  for (var i = 0; i < arryLengthB; i++) {
    Logger.log("count: " + i);
    thisIterationObj = {}; //reset to empty
    thisIterationObj = B[i];
    Logger.log("thisIterationObj: " + thisIterationObj);

    //Create a new object of all values in array B
    for (var thisKey2 in thisIterationObj) {
      Logger.log("thisKey2: " + thisKey2);
      Logger.log(thisIterationObj[thisKey2]);

      newObjB[thisKey2] = thisIterationObj[thisKey2];
      Logger.log(newObjB[thisKey2]);
    }
  };

  //Compare Object A to Object B
  for (var keyObjA in newObjA) {
    thisIterationObj = {};
    //Find key of objA in objB

    var theMatch = newObjB[keyObjA];
    Logger.log("newObjB[keyObjA]: " + newObjB[keyObjA]);

    if (theMatch != undefined) {
      Logger.log("newObjB[keyObjA] " + newObjB[keyObjA]);
      thisIterationObj[keyObjA] = newObjA[keyObjA];
      finalArry.push(thisIterationObj);
    };
  };

  //Test the final result by looping through and displaying all values in the final array
  Logger.log("finalArry: " + finalArry);
  Logger.log("finalArry length: " + finalArry.length);

  //This code is a check to test for the correct result
  for (var i = 0; i < finalArry.length; i++) {
    thisIterationObj = {}; //reset to blank
    thisIterationObj = finalArry[i];

    for (var finalKey in thisIterationObj)
    Logger.log("finalArry value: " + i + ": " + finalKey + " : " + thisIterationObj[finalKey]);

  };
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM