使用javascript连接两个数据集

时间:2017-09-30 18:11:43

标签: javascript arrays dataset javascript-objects

我有两个看起来像这样的数据集:

var gatePos = [ 
    { gate: 1, x: 1177, y: 200 },
    { gate: 2, x: 1109, y: 200 },
    { gate: 3, x: 1042, y: 200 },
    { gate: 4, x: 975, y: 200 },
    { gate: 5, x: 908, y: 200 },
    { gate: 6, x: 842, y: 200 },
    { gate: 7, x: 774, y: 200 },
    { gate: 8, x: 708, y: 200 },
    { gate: 9, x: 641, y: 200 },
    { gate: 10, x: 578, y: 200 }
 ];

[
  {
    "gate": "8B",
    "value": 126
  },
  {
    "gate": "9B",
    "value": 268
  },
  {
    "gate": "10B",
    "value": 91
  },
  {
    "gate": "21B",
    "value": 9
  },
  {
    "gate": "24B",
    "value": 1
  },
  {
    "gate": "JC",
    "value": 48352
  },
  {
    "gate": "LOCALISER",
    "value": 22
  },
  {
    "gate": 1,
    "value": 34351
  },
  {
    "gate": 2,
    "value": 37855
  },
  {
    "gate": 3,
    "value": 38462
  },
  {
    "gate": 4,
    "value": 38126
  },
  {
    "gate": 5,
    "value": 40089
  }

,
  {
    "gate": 6,
    "value": 39295
  },
  {
    "gate": 7,
    "value": 36581
  },
  {
    "gate": 8,
    "value": 33908
  },
  {
    "gate": 9,
    "value": 31187
  },
  {
    "gate": 10,
    "value": 22915
  },
  {
    "gate": 11,
    "value": 5164
  },
  {
    "gate": 12,
    "value": 9533
  },
  {
    "gate": 13,
    "value": 6454
  },
  {
    "gate": 14,
    "value": 5003
  },
  {
    "gate": 15,
    "value": 1
  },
  {
    "gate": 21,
    "value": 19804
  },
  {
    "gate": 22,
    "value": 21239
  },
  {
    "gate": 23,
    "value": 17779
  },
  {
    "gate": 24,
    "value": 15213
  },
  {
    "gate": "-",
    "value": 37562
  }
]

他们以“门”为关键。 我希望在SQL术语“数据集”中“左连接”,并且输出如下:

[{ gate: 1, x: 100, y: 200, value: 999 },
{ gate: 2, x: 150, y: 200, value: 1000}] 
... etc

有人能指出我正确的方向来实现这一目标吗?我主要使用d3.js,但我知道它不支持这种类型的东西所以我想在标准的js中实现,即使你可以给我一些术语来搜索或者某些东西会是一个巨大的帮助。

如果您不熟悉SQL的左连接,我希望我的新数组包含来自var gatePos的所有值,但只包含来自第二个数据集的MATCHING值。

3 个答案:

答案 0 :(得分:2)

您可以使用函数和哈希表来模仿left outer join具有相同连接键的项目。



var gatePos = [{ gate: 1, x: 1177, y: 200 }, { gate: 2, x: 1109, y: 200 }, { gate: 3, x: 1042, y: 200 }, { gate: 4, x: 975, y: 200 }, { gate: 5, x: 908, y: 200 }, { gate: 6, x: 842, y: 200 }, { gate: 7, x: 774, y: 200 }, { gate: 8, x: 708, y: 200 }, { gate: 9, x: 641, y: 200 }, { gate: 10, x: 578, y: 200 }],
    gateValues = [{ gate: "8B", value: 126 }, { gate: "9B", value: 268 }, { gate: "10B", value: 91 }, { gate: "21B", value: 9 }, { gate: "24B", value: 1 }, { gate: "JC", value: 48352 }, { gate: "LOCALISER", value: 22 }, { gate: 1, value: 34351 }, { gate: 2, value: 37855 }, { gate: 3, value: 38462 }, { gate: 4, value: 38126 }, { gate: 5, value: 40089 }, { gate: 6, value: 39295 }, { gate: 7, value: 36581 }, { gate: 8, value: 33908 }, { gate: 9, value: 31187 }, { gate: 10, value: 22915 }, { gate: 11, value: 5164 }, { gate: 12, value: 9533 }, { gate: 13, value: 6454 }, { gate: 14, value: 5003 }, { gate: 15, value: 1 }, { gate: 21, value: 19804 }, { gate: 22, value: 21239 }, { gate: 23, value: 17779 }, { gate: 24, value: 15213 }, { gate: "-", value: 37562 }],
    result = function (left, right, on, key) {
        var hash = Object.create(null),
            result = left.map(function (o) {
                return hash[o[on]] = Object.assign({}, o);
            });
        right.forEach(function (o) {
            if (hash[o[on]]) {
                hash[o[on]][key] = o[key];
            }
        });
        return result;
    }(gatePos, gateValues, 'gate', 'value');

console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }




答案 1 :(得分:1)

检查此代码是否适合您:

app.use("/", posts);

答案 2 :(得分:1)

您可以使用resucefind之类的:

let result = gatePos.reduce(function(res, obj) {     // for each object obj in gatePos array
    var found = otherArray.find(function(o) {        // check if there is an object in the other array (rename the variable name before use)
        return o.gate == obj.gate;                   // that has the same gate as the object obj
    });
    if(found) {                                      // if we found one
        var newObj = Object.assign({}, obj);         // then create a new object
        newObj.value = found.value;                  // set its value
        res.push(newObj);                            // and add it to the result array
    }
    return res;
}, []);

注意:我将您问题中的第二个数组称为otherArray。重命名变量名称以匹配用于保存该数组的实际变量。