比较jQuery中的两个对象并获得差异[重复]

时间:2022-08-18 14:16:44

This question already has an answer here:

这个问题在这里已有答案:

Using jQuery I would like to compare 2 objects:

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

sourceArray:

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

destination array

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

What I would like to do, is compare the target object with the source object based on the ID and find the mis-matched entries with a description on the resultant object. So the result will look like this:

我想要做的是,根据ID比较目标对象和源对象,找到不匹配的条目以及对结果对象的描述。所以结果看起来像这样:

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

Any quick help is really appreciated.

任何快速帮助真的很感激。

5 个解决方案

#1


This isn't a good use of jQuery, but here is some vanilla javascript that does what you want.

这不是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/

#2


If you are wanting to dedupe your array, this will work:

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

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

Fiddle: https://jsfiddle.net/Ljzor9c6/

If you are only wanting items that were duped, you can easily invert the condition:

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

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

#3


You can loop through the items in the first array and put the ID's in a map, then loop through the items in the second array and remove the matching ID's and add the missing.

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

Then just loop through the map to create the objects in the resulting array:

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

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 * snippet
document.write(JSON.stringify(resultArray));

#4


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: Object
ID: 345
Name: "Double"
URL: "yyy"
desc: "missing in destination"

0:对象ID:345名称:“双”URL:“yyy”desc:“目标中缺失”

1: Object
ID: 888
Name: "Double"
URL: "yyy"
desc: "missing in origin"

1:对象ID:888名称:“双”URL:“yyy”desc:“缺少原产地”

jsfiddle DEMO

#5


For things like this, you should use lodash. With lodash you can just do this:

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

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

#1


This isn't a good use of jQuery, but here is some vanilla javascript that does what you want.

这不是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/

#2


If you are wanting to dedupe your array, this will work:

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

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

Fiddle: https://jsfiddle.net/Ljzor9c6/

If you are only wanting items that were duped, you can easily invert the condition:

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

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

#3


You can loop through the items in the first array and put the ID's in a map, then loop through the items in the second array and remove the matching ID's and add the missing.

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

Then just loop through the map to create the objects in the resulting array:

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

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 * snippet
document.write(JSON.stringify(resultArray));

#4


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: Object
ID: 345
Name: "Double"
URL: "yyy"
desc: "missing in destination"

0:对象ID:345名称:“双”URL:“yyy”desc:“目标中缺失”

1: Object
ID: 888
Name: "Double"
URL: "yyy"
desc: "missing in origin"

1:对象ID:888名称:“双”URL:“yyy”desc:“缺少原产地”

jsfiddle DEMO

#5


For things like this, you should use lodash. With lodash you can just do this:

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

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