比较jQuery中的两个对象并获得不同

时间:2015-05-12 15:53:13

标签: javascript jquery arrays json

使用jQuery我想比较两个对象:

sourceArray:

var origArray = [{
    "Name": "Single",
    "URL": "xxx",
    "ID": 123
},
{
    "Name": "Double",
    "URL": "yyy",
    "ID":  345
},
{
    "Name": "Family",
    "URL": "zzz",
    "ID": 567
}];

目标数组

var destArray = [{
    "Name": "Single",
    "URL": "xxx",
    "ID": 123
},
{
    "Name": "Double",
    "URL": "yyy",
    "ID":  888
},
{
    "Name": "Family",
    "URL": "zzz",
    "ID": 567
}];

我想做的是,根据ID将目标对象与源对象进行比较,并找到不匹配的条目以及对结果对象的描述。所以结果将如下所示:

var resultArray = [{
    "Name": "Double",
    "URL": "yyy",
    "ID":  888,
    "desc": "missing in source" 
},
{
    "Name": "Double",
    "URL": "yyy",
    "ID": 345,
    "desc": "missing in destination"
}];

非常感谢任何快速帮助。

5 个答案:

答案 0 :(得分:3)

这不是jQuery的好用,但是这里有一些你想要的vanilla javascript。

function objDiff(array1, array2) {
    var resultArray = []

    array2.forEach(function(destObj) {
        var check = array1.some(function(origObj) {
            if(origObj.ID == destObj.ID) return true
        })
        if(!check) {
            destObj.desc = 'missing in source'
            resultArray.push(destObj)
        }
    })

    array1.forEach(function(origObj) {
        var check = array2.some(function(destObj) {
            if(origObj.ID == destObj.ID) return true
        })
        if(!check) {
            origObj.desc = 'missing in destination'
            resultArray.push(origObj)
        }
    })

    return resultArray
}

https://jsfiddle.net/9gaxsLbz/1/

答案 1 :(得分:0)

如果您想重复数组,那么这将有效:

var merged = origArray.concat(destArray);
var unique = merged.filter(function(item) {
    return ~this.indexOf(item.ID) ? false : this.push(item.ID);
}, []);

小提琴:https://jsfiddle.net/Ljzor9c6/

如果您只想要被欺骗的物品,您可以轻松地改变条件:

var merged = origArray.concat(destArray);
var dupes  = merged.filter(function(item) {
    return ~this.indexOf(item.ID) ? true : !this.push(item.ID);
}, []);

答案 2 :(得分:0)

您可以遍历第一个数组中的项目并将ID放入地图中,然后遍历第二个数组中的项目并删除匹配的ID并添加缺失的内容。

然后循环遍历地图以在结果数组中创建对象:

var origArray = [{
    "Name": "Single",
    "URL": "xxx",
    "ID": 123
},
{
    "Name": "Double",
    "URL": "yyy",
    "ID":  345
},
{
    "Name": "Family",
    "URL": "zzz",
    "ID": 567
}];

var destArray = [{
    "Name": "Single",
    "URL": "xxx",
    "ID": 123
},
{
    "Name": "Double",
    "URL": "yyy",
    "ID":  888
},
{
    "Name": "Family",
    "URL": "zzz",
    "ID": 567
}];

var map = {};
for (var i = 0; i < origArray.length; i++) {
    map[origArray[i].ID] = 'source';
}
for (var i = 0; i < destArray.length; i++) {
    var id = destArray[i].ID;
    if (id in map) {
        delete map[id];
    } else {
        map[id] = 'destination';
    }
}
var resultArray = [];
for (key in map) {
    var arr = map[key] == 'source' ? origArray : destArray;
    for (var i = 0; arr[i].ID != key; i++) ;
    resultArray.push({
        Name: arr[i].Name,
        URL: arr[i].URL,
        ID: arr[i].ID,
        desc: 'missing in ' + map[key]
    });
}

// show result in StackOverflow snippet
document.write(JSON.stringify(resultArray));

答案 3 :(得分:0)

var result = [];

for(var i = 0; i < oa.length; i++) {
    var idx = mIndexOf(oa[i].ID);
    if(idx > -1) {
        oa.splice(i, 1);
        da.splice(idx, 1);
    }
}

for(var i = 0; i < oa.length; i++) {
    var ln = result.length;
    result[ln] = oa[i];
    result[ln].desc = "missing in destination";
}
for(var i = 0; i < da.length; i++) {
    var ln = result.length;
    result[ln] = da[i];
    result[ln].desc = "missing in origin";
}

function mIndexOf(id) {
    for(var i = 0; i < oa.length; i++)
        if(oa[i].ID == id)
            return i;
    return -1;
}

console.log(result);
  

0:对象
  ID:345
  姓名:&#34; Double&#34;
  网址:&#34; yyy&#34;
  desc:&#34;在目的地失踪&#34;

     

1:对象
  ID:888
  姓名:&#34; Double&#34;
  网址:&#34; yyy&#34;
  desc:&#34;缺少原产地&#34;

jsfiddle DEMO

答案 4 :(得分:-1)

对于这样的事情,你应该使用lodash。使用lodash,你可以这样做:

var resultArray = _.defaults(destArray, origArray);