Compare array objects and show difference

北城以北 提交于 2019-12-31 03:24:08

问题


I have two arrays which I want to compare and check if there is an deleted item in one of these arrays. If there is show me the difference (deleted item)

Here is the code below how I would like to achieve this:

 var completedList = [{id:1},{id:2},{id:3},{id:4},{id:7},{id:8}];
    var invalidList = [{id:3},{id:4},{id:5},{id:6}];

    // filter the items from the invalid list, out of the complete list
    var validList = completedList.map((item) => {
        console.log(item.id)
        return item.id;
        //console.log(invalidList.id);
    }).filter(item => {
        Object.keys(invalidList).map(key => {
            console.log(invalidList[key].id)
            //return !invalidList[key].id.includes(item.id);
        });
    })

    console.log(validList); // Print [1,2,7,8]

    // get a Set of the distinct, valid items
    var validItems = new Set(validList);

But this returns me a lot of id's how can I map through both array's and filter on object property id? And only show the difference between these array objects.

So basically what I expect is to see the difference between those arrays so log the differences in id's so in this example: 1,2,5,6,7,8


回答1:


You could take a Set for getting a difference. For getting the differences from each other (a symmetric difference), you need to get both differences.

const
    difference = (a, b) => Array.from(b.reduce((s, v) => (s.delete(v), s), new Set(a))),
    getId = ({ id }) => id;

var completedList = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, { id: 7 }, { id: 8 }],
    invalidList = [{ id: 3 }, { id: 4 }, { id: 5 }, { id: 6 }],
    complete = completedList.map(getId),
    invalid = invalidList.map(getId),
    left = difference(complete, invalid),
    right = difference(invalid, complete),
    result = [...left, ...right]

console.log(result.join(' '));
console.log(left.join(' '));
console.log(right.join(' '));



回答2:


This should do the trick.

let completedList = [{id:1},{id:2},{id:3},{id:4},{id:7},{id:8}];
let invalidList = [{id:3},{id:4},{id:5},{id:6}];
// filter the items from the invalid list, out of the complete list
let temp1 = completedList.map(e => e.id);
let temp2 = invalidList.map(e => e.id);
let validList = temp1.filter(e => temp2.indexOf(e) === -1);
// find items only in invalidList
let difference = temp2.filter(e => temp1.indexOf(e) === -1);
console.log(validList); // Print [1,2,7,8]
console.log(difference);



回答3:


I often rely on lodash implementation for comparison. In lo dash you can get the job done following manner

_.intersectionWith(arr1, arr2, _.isEqual) - For similarity _.differenceWith(arr1, arr2, _.isEqual) - for differences

This ans is confined to using a util library to get the job done. If you are looking for the exact algo I would definitely take some time to develop it and reply as a comment to this post .

Thanks




回答4:


var completedList = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, { id: 7 }, { id: 8 }];
var invalidList = [{ id: 3 }, { id: 4 }, { id: 5 }, { id: 6 }];

//get the items that are in the invalid list but not completed list
var filteredList1 = invalidList.filter((invalidListItem) => !completedList.find((item) => item.id === invalidListItem.id));

//get the items that are in  the completed list but not in the invalid list
var filteredList2 = completedList.filter((completedListItem) => !invalidList.find((item) => item.id === completedListItem.id));

//join the two arrays
var difference = filteredList1.concat(filteredList2);

//display the merged array and sort
console.log(difference.sort((item1, item2) => { return item1.id > item2.id ? 1 : item1.id < item2.id ? -1 : 0; }));

//outputs 1,2,5,6,7,8


来源:https://stackoverflow.com/questions/52535839/compare-array-objects-and-show-difference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!