How to get the difference between two arrays of objects in JavaScript

前端 未结 18 1059
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 04:45

I have two result sets like this:

// Result 1
[
    { value: \"0\", display: \"Jamsheer\" },
    { value: \"1\", display: \"Muhammed\" },
    { value: \"2\",         


        
相关标签:
18条回答
  • 2020-11-22 05:09

    I found this solution using filter and some.

    resultFilter = (firstArray, secondArray) => {
      return firstArray.filter(firstArrayItem =>
        !secondArray.some(
          secondArrayItem => firstArrayItem._user === secondArrayItem._user
        )
      );
    };

    0 讨论(0)
  • 2020-11-22 05:11

    You can create an object with keys as the unique value corresponding for each object in array and then filter each array based on existence of the key in other's object. It reduces the complexity of the operation.

    ES6

    let a = [{ value:"4a55eff3-1e0d-4a81-9105-3ffffd7521d642", display:"Jamsheer"}, { value:"644838b3-604d-4899-8b78-09e4799f586f", display:"Muhammed"}, { value:"b6ee537a-375c-45bd-b9d4-4dd84a75041d", display:"Ravi"}, { value:"e97339e1-939d-47ab-974c-1b68c9cfb536", display:"Ajmal"},  { value:"a63a6f77-c637-454e-abf2-dfb9b543af6c", display:"Ryan"}];
    let b = [{ value:"4a55eff3-1e0d-4a81-9105-3ffffd7521d642", display:"Jamsheer", $$hashKey:"008"}, { value:"644838b3-604d-4899-8b78-09e4799f586f", display:"Muhammed", $$hashKey:"009"}, { value:"b6ee537a-375c-45bd-b9d4-4dd84a75041d", display:"Ravi", $$hashKey:"00A"}, { value:"e97339e1-939d-47ab-974c-1b68c9cfb536", display:"Ajmal", $$hashKey:"00B"}];
    
    let valuesA = a.reduce((a,{value}) => Object.assign(a, {[value]:value}), {});
    let valuesB = b.reduce((a,{value}) => Object.assign(a, {[value]:value}), {});
    let result = [...a.filter(({value}) => !valuesB[value]), ...b.filter(({value}) => !valuesA[value])];
    console.log(result);

    ES5

    var a = [{ value:"4a55eff3-1e0d-4a81-9105-3ffffd7521d642", display:"Jamsheer"}, { value:"644838b3-604d-4899-8b78-09e4799f586f", display:"Muhammed"}, { value:"b6ee537a-375c-45bd-b9d4-4dd84a75041d", display:"Ravi"}, { value:"e97339e1-939d-47ab-974c-1b68c9cfb536", display:"Ajmal"},  { value:"a63a6f77-c637-454e-abf2-dfb9b543af6c", display:"Ryan"}];
    var b = [{ value:"4a55eff3-1e0d-4a81-9105-3ffffd7521d642", display:"Jamsheer", $$hashKey:"008"}, { value:"644838b3-604d-4899-8b78-09e4799f586f", display:"Muhammed", $$hashKey:"009"}, { value:"b6ee537a-375c-45bd-b9d4-4dd84a75041d", display:"Ravi", $$hashKey:"00A"}, { value:"e97339e1-939d-47ab-974c-1b68c9cfb536", display:"Ajmal", $$hashKey:"00B"}];
    
    var valuesA = a.reduce(function(a,c){a[c.value] = c.value; return a; }, {});
    var valuesB = b.reduce(function(a,c){a[c.value] = c.value; return a; }, {});
    var result = a.filter(function(c){ return !valuesB[c.value]}).concat(b.filter(function(c){ return !valuesA[c.value]}));
    console.log(result);

    0 讨论(0)
  • 2020-11-22 05:12

    you can do diff a on b and diff b on a, then merge both results

    let a = [
        { value: "0", display: "Jamsheer" },
        { value: "1", display: "Muhammed" },
        { value: "2", display: "Ravi" },
        { value: "3", display: "Ajmal" },
        { value: "4", display: "Ryan" }
    ]
    
    let b = [
        { value: "0", display: "Jamsheer" },
        { value: "1", display: "Muhammed" },
        { value: "2", display: "Ravi" },
        { value: "3", display: "Ajmal" }
    ]
    
    // b diff a
    let resultA = b.filter(elm => !a.map(elm => JSON.stringify(elm)).includes(JSON.stringify(elm)));
    
    // a diff b
    let resultB = a.filter(elm => !b.map(elm => JSON.stringify(elm)).includes(JSON.stringify(elm)));  
    
    // show merge 
    console.log([...resultA, ...resultB]);

    0 讨论(0)
  • 2020-11-22 05:13

    I take a slightly more general-purpose approach, although similar in ideas to the approaches of both @Cerbrus and @Kasper Moerch. I create a function that accepts a predicate to determine if two objects are equal (here we ignore the $$hashKey property, but it could be anything) and return a function which calculates the symmetric difference of two lists based on that predicate:

    a = [{ value:"4a55eff3-1e0d-4a81-9105-3ffffd7521d642", display:"Jamsheer"}, { value:"644838b3-604d-4899-8b78-09e4799f586f", display:"Muhammed"}, { value:"b6ee537a-375c-45bd-b9d4-4dd84a75041d", display:"Ravi"}, { value:"e97339e1-939d-47ab-974c-1b68c9cfb536", display:"Ajmal"},  { value:"a63a6f77-c637-454e-abf2-dfb9b543af6c", display:"Ryan"}]
    b = [{ value:"4a55eff3-1e0d-4a81-9105-3ffffd7521d642", display:"Jamsheer", $$hashKey:"008"}, { value:"644838b3-604d-4899-8b78-09e4799f586f", display:"Muhammed", $$hashKey:"009"}, { value:"b6ee537a-375c-45bd-b9d4-4dd84a75041d", display:"Ravi", $$hashKey:"00A"}, { value:"e97339e1-939d-47ab-974c-1b68c9cfb536", display:"Ajmal", $$hashKey:"00B"}]
    
    var makeSymmDiffFunc = (function() {
        var contains = function(pred, a, list) {
            var idx = -1, len = list.length;
            while (++idx < len) {if (pred(a, list[idx])) {return true;}}
            return false;
        };
        var complement = function(pred, a, b) {
            return a.filter(function(elem) {return !contains(pred, elem, b);});
        };
        return function(pred) {
            return function(a, b) {
                return complement(pred, a, b).concat(complement(pred, b, a));
            };
        };
    }());
    
    var myDiff = makeSymmDiffFunc(function(x, y) {
        return x.value === y.value && x.display === y.display;
    });
    
    var result = myDiff(a, b); //=>  {value="a63a6f77-c637-454e-abf2-dfb9b543af6c", display="Ryan"}
    

    It has one minor advantage over Cerebrus's approach (as does Kasper Moerch's approach) in that it escapes early; if it finds a match, it doesn't bother checking the rest of the list. If I had a curry function handy, I would do this a little differently, but this works fine.

    Explanation

    A comment asked for a more detailed explanation for beginners. Here's an attempt.

    We pass the following function to makeSymmDiffFunc:

    function(x, y) {
        return x.value === y.value && x.display === y.display;
    }
    

    This function is how we decide that two objects are equal. Like all functions that return true or false, it can be called a "predicate function", but that's just terminology. The main point is that makeSymmDiffFunc is configured with a function that accepts two objects and returns true if we consider them equal, false if we don't.

    Using that, makeSymmDiffFunc (read "make symmetric difference function") returns us a new function:

            return function(a, b) {
                return complement(pred, a, b).concat(complement(pred, b, a));
            };
    

    This is the function we will actually use. We pass it two lists and it finds the elements in the first not in the second, then those in the second not in the first and combine these two lists.

    Looking over it again, though, I could definitely have taken a cue from your code and simplified the main function quite a bit by using some:

    var makeSymmDiffFunc = (function() {
        var complement = function(pred, a, b) {
            return a.filter(function(x) {
                return !b.some(function(y) {return pred(x, y);});
            });
        };
        return function(pred) {
            return function(a, b) {
                return complement(pred, a, b).concat(complement(pred, b, a));
            };
        };
    }());
    

    complement uses the predicate and returns the elements of its first list not in its second. This is simpler than my first pass with a separate contains function.

    Finally, the main function is wrapped in an immediately invoked function expression (IIFE) to keep the internal complement function out of the global scope.


    Update, a few years later

    Now that ES2015 has become pretty well ubiquitous, I would suggest the same technique, with a lot less boilerplate:

    const diffBy = (pred) => (a, b) => a.filter(x => !b.some(y => pred(x, y)))
    const makeSymmDiffFunc = (pred) => (a, b) => diffBy(pred)(a, b).concat(diffBy(pred)(b, a))
    
    const myDiff = makeSymmDiffFunc((x, y) => x.value === y.value && x.display === y.display)
    
    const result = myDiff(a, b)
    //=>  {value="a63a6f77-c637-454e-abf2-dfb9b543af6c", display="Ryan"}
    
    0 讨论(0)
  • 2020-11-22 05:13

    I think the @Cerbrus solution is spot on. I have implemented the same solution but extracted the repeated code into it's own function (DRY).

     function filterByDifference(array1, array2, compareField) {
      var onlyInA = differenceInFirstArray(array1, array2, compareField);
      var onlyInb = differenceInFirstArray(array2, array1, compareField);
      return onlyInA.concat(onlyInb);
    }
    
    function differenceInFirstArray(array1, array2, compareField) {
      return array1.filter(function (current) {
        return array2.filter(function (current_b) {
            return current_b[compareField] === current[compareField];
          }).length == 0;
      });
    }
    
    0 讨论(0)
  • 2020-11-22 05:14

    For those who like one-liner solutions in ES6, something like this:

    const arrayOne = [ 
      { value: "4a55eff3-1e0d-4a81-9105-3ffffd7521d642", display: "Jamsheer" },
      { value: "644838b3-604d-4899-8b78-09e4799f586f", display: "Muhammed" },
      { value: "b6ee537a-375c-45bd-b9d4-4dd84a75041d", display: "Ravi" },
      { value: "e97339e1-939d-47ab-974c-1b68c9cfb536", display: "Ajmal" },
      { value: "a63a6f77-c637-454e-abf2-dfb9b543af6c", display: "Ryan" },
    ];
              
    const arrayTwo = [
      { value: "4a55eff3-1e0d-4a81-9105-3ffffd7521d642", display: "Jamsheer"},
      { value: "644838b3-604d-4899-8b78-09e4799f586f", display: "Muhammed"},
      { value: "b6ee537a-375c-45bd-b9d4-4dd84a75041d", display: "Ravi"},
      { value: "e97339e1-939d-47ab-974c-1b68c9cfb536", display: "Ajmal"},
    ];
    
    const results = arrayOne.filter(({ value: id1 }) => !arrayTwo.some(({ value: id2 }) => id2 === id1));
    
    console.log(results);

    0 讨论(0)
提交回复
热议问题