通过与其他 table 的列进行比较来增加列数据

Increment column data by comparing with columns from other table

我手头有一个非常复杂的计算,但我发现很难完成。这里是。我有 3 table.

Table1:(ID1和Sum是table的列)

ID1    | Sum
123456 |  
345678 |

表2:(城市,ID2是table的列)

City |  ID2     
 NY  | 123456        
 CA  | 345678         
 TX  | 12345         
 BF  | 34567  

表3:(城市,ID3是table的列)

City  |   ID3 
 LA   | 123456
 NB   | 12345   

表 1 的最终输出应如下所示。

根据 Table2 和 Table3 的值,Table1 应该如下所示

 ID1   | Sum
123456 |   4
345678 |   2

我先解释一下场景。我想更新 'Table1' 的列 'Sum'。这将通过将 'Table1' 的 'ID1' 列的前 5 个字符与 'Table2' 和 'ID3' 列的 'ID2' 列中存在的列数据的前 5 个字符进行比较来更新共 'Table3'

例如Table1的'12345'(注意实际值为'123456')与Table2和Table3的列数据进行比较。因此最终表 1

中总和为 4

table 的输入格式如下 Table1 :- (Table2 和 Table3 的输入形式相似)

[Object][Object]
[0-1]
  [0]: Object
      ID1: '123456'
      Sum:
  [1]: Object
      ID1: '345678'
      Sum:

尝试过的解决方案:

function count(key) {
return function (r, a) {
    r[a[key]] = (r[a[key]] || 0) + 1;
    return r;
};
}

Table1.forEach(function (a) {
a.Sum = this[a.ID1] || '-';
}, Table2.reduce(count('ID2'), Table3.reduce(count('ID3'), Object.create(null))));

上述解决方案将ID1的值与ID2和ID3的值进行比较。我只想使用数据的前 5 个字符,然后如上所述进行比较。有人可以让我知道我的代码需要如何编辑才能解决我的问题。

可能有比这更好的方法...一个选择是

var table1=[{id:'123456',sum:0},{id:'345678',sum:0},{id:'345',sum:0}];
var tables =  {"table2":[{id1:'12345'},{id1:'12345'},{id1:'1234567'},{id1:'345'},{id1:'34567'},{id1:'3'}],"table3":[{id1:'12345'},{id1:'12345'},{id1:'1234567'},{id1:'34567'},{id1:'3'}],"table4":[],"table5":null,"table6":"","table7":[{id1:'12345'},{id1:'12345'},{id1:'1234567'},{id1:'34567'},{id1:'3'}]};

//foreach key/value in the table1 object
_.each(table1, function(a, b) {
  var current = a.id;
  a.sum = 0;
    //foreach key/value in the tables 
  _.each(tables, function(c, d) {    
    //add check, expand this as required
    if (c != undefined && c instanceof Array) {
      a.sum += findTotal(c, current);
    }
  });
});

function findTotal(tableToWork, currentId) {
  var sum = _.countBy(tableToWork, function(e, f) {
    return e.id1.substring(0, 4) == currentId.substring(0, 4)
  });
  return sum.true == null ? 0 : sum.true;
}
document.getElementById('p').innerHTML = JSON.stringify(table1)
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<p id="p">
  Hi
</p>

让我们知道。

假设您有一个 aggTable 和一个源表数组 srcTables 及其关联的 id 列,通用解决方案如下:

var aggTable = Table1;
var srcTables = [{ table: Table2, id: 'ID2' }, { table: Table3, id: 'ID3' }]; // can add as many as needed

// returns the key portion of the ID
function getKey(id) {
  return id.substring(0, 4);  
}

// count IDs inside one source table
function countIDs(table, id) {
   return _.countBy(table, function(row) { return getKey(row[id]); });
}

function aggregate(aggTable, srcTables) {
   // yields the following result: [{ 12345: 2, 34567: 2 }, { 12345: 2 }]
   var idCounts = _.map(srcTables, function(entry) { return countIDs(entry.table, entry.id) });

   // aggregate counts into the aggTable
   _.each(aggTable, function(row) {
     var id = getKey(row['ID1']);
     row['Sum'] = _.reduce(idCounts, function(memo, idCount) {
                      return (_.has(idCount, id)) ? memo + idCount[id] : memo;
                    }, 0);
   });
}