2

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

5
  • does order matters? please add the wanted result as well. Commented Sep 27, 2018 at 11:25
  • and what is the acceptable complexity of the needed solution? Commented Sep 27, 2018 at 11:33
  • @NinaScholz the other question is not a duplicate. It was after a thought not the expected output I wanted to realise Commented Sep 27, 2018 at 11:51
  • and why do you take my answer as problem description? Commented Sep 27, 2018 at 11:51
  • @Sireini please see my answer Commented Sep 27, 2018 at 12:18

4 Answers 4

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(' '));

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

and if I only want to print the difference of the invalidList
0

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

Comments

0
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.