Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:基于多个键从对象数组中确定并返回重复的对象_Javascript_Arrays_Javascript Objects - Fatal编程技术网

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

Javascript:基于多个键从对象数组中确定并返回重复的对象,javascript,arrays,javascript-objects,Javascript,Arrays,Javascript Objects,我有一个对象数组: var data = [{"monitor":"TFT", "manufacturer":"MONCORP", "monID":"1234", "Delivery Way":"DELIVERY", "BarCode Text":"Test", "BarCode Id":"D9",

我有一个对象数组:

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

在数据库级别执行此操作可能是一个好主意,例如,某种分组操作

在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];
}
为了将重复记录组合在一起,您可以构造一个映射键->重复项数组。可以这样做,只显示循环的内部

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

“我想根据键的值来区分它们:……”您的意思是要忽略“条形码文本”的差异吗?或者列表中没有的任何其他键?是的,我只对列表感兴趣:监视器、制造商、monID、交付方式、条形码Id、状态。此方法工作良好,常用。如果数据中的值可以是字符串以外的值,请不要忘记对其进行一些调整。对于对象或数组,可以使用JSON.stringify。在这种情况下,您应该按照建议使用哈希代码,以避免过长的字符串扫描我们以某种方式删除了这些行我提到过的这些行预期的结果?对不起,您删除这些行是什么意思…?拒绝这些行实际将其存储在日志文件中,这些日志文件具有相同的密钥值:监视器、制造商、monID、交付方式,条形码Id,状态。@AlexBotzis是,在以var key=data[i]开头的行中。监视器。您可以在此处明确指定用于组合密钥的字段。只是不要把你想忽略的字段放在这里
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]);
}