Javascript:从基于多个键的对象数组中确定并返回重复对象

时间:2017-06-29 08:54:50

标签: javascript arrays javascript-objects

我有一个对象数组:

    var data  = [{"monitor":"TFT",
                  "manufacturer":"MONCORP",
                  "monID":"1234",
                  "Delivery Way":"DELIVERY",
                  "BarCode Text":"Test",
                  "BarCode Id":"D9",
                  "Status":"OK"},               

                 {"monitor":"LCD",
                  "manufacturer":"MONCORP",
                  "monID":"",
                  "Delivery Way":"PICKUP",
                  "BarCode Text":"Dummy Text",
                  "BarCode Id":"P2",
                  "Status":"OK"},

                 {"monitor":"TFT",
                  "manufacturer":"MONCORP",
                  "monID":"1234",
                  "Delivery Way":"DELIVERY",
                  "BarCode Text":"ONLY TEST",
                  "BarCode Id":"D9",
                  "Status":"OK"}, 

                 {"monitor":"LCD",
                  "manufacturer":"MONCORP",
                  "monID":"1234",
                  "Delivery Way":"DELIVERY",
                  "BarCode Text":"FOR TESTING PURPOSE",
                  "BarCode Id":"D9",
                  "Status":"OK"},

                  {"monitor":"TFT",
                   "manufacturer":"MONCORP",
                   "monID":"",
                   "Delivery Way":"PICKUP",
                   "BarCode Text":"DUMMIEST TEXT",
                   "BarCode Id":"P7",
                   "Status":"OK"}];

所以我想只采取重复的对象,但我想根据键的值来区分它们:监视器,制造商,monID​​,传递方式,条形码ID,状态。

预期结果是:

    expected = [{"monitor":"TFT",
                 "manufacturer":"MONCORP",
                 "monID":"1234",
                 "Delivery Way":"DELIVERY",
                 "BarCode Text":"Test",
                 "BarCode Id":"D9",
                 "Status":"OK"},

                {"monitor":"TFT",
                 "manufacturer":"MONCORP",
                 "monID":"1234",
                 "Delivery Way":"DELIVERY",
                 "BarCode Text":"ONLY TEST",
                 "BarCode Id":"D9",
                 "Status":"OK"}]

1 个答案:

答案 0 :(得分:1)

在数据库级别上执行此操作可能是个好主意(例如,某种GROUP BY操作)。

在JavaScript中,您可以遍历数组并为每条记录创建一个哈希值,该哈希值应由您想要唯一的字段组成。然后可以将此哈希用作映射键,即将这些记录插入具有此键的映射将消除重复。

示例:

var map = {};
for (var i = 0; i < data.length; i++) {
    var key = data[i].monitor + "#" + data[i].monID + "#" + data[i].manufacturer + ... ;
    map[key] = data[i];
}

map对象最后只包含使用此键映射到 last 项目的唯一键。

请注意,key只是一个字符串,它连接了您想要唯一的字段。如果您的字段包含字符#,或者它们不是字符串,则无法正常工作。如果你想这样,我建议计算一个hash code from the string

识别重复项,您可以在迭代的每一步检查key是否已经在地图中:

if (map[key]) {
   // This record is a duplicate
}

为了将重复记录组合在一起,您可以构建地图映射键 - &gt;重复数组。这可以这样做(仅显示循环内部)

var key = ...
if (!map[key]) {
    // First time we see this key, let's add it to the map
    map[key] = [ data[i] ]; // Map the key to a new array containing the current record
} else {
    // Duplicate; just add this record to the existing 
    // list of records with the same key
    map[key].push(data[i]);
}