Union of Array of Objects in JavaScript?

前端 未结 6 912
予麋鹿
予麋鹿 2020-12-03 12:11

So after searching the interwebz for a few hours I have not found the solution I am looking for.

I have two arrays that contain game objects with a lot of informatio

相关标签:
6条回答
  • 2020-12-03 12:42

    jQuery has the method extend:

    $.extend(true, object1, object2);
    
    0 讨论(0)
  • 2020-12-03 12:44

    Using underscore extend, you can do:

    var objects = [{ bar : 1, nuts : 2} , {foo : 3, nuts : 4}]
    _.extend.apply(this,objects)
    

    If the list can be empty, make sure to append an empty object to it.

    0 讨论(0)
  • 2020-12-03 12:48

    Set (ES6/ES2015) will help you.

    const info1 = {id: 1}
    const info2 = {id: 2}
    const info3 = {id: 3}
    
    const array1 = [info1, info2]
    const array2 = [info1, info3]
    
    const union = [...new Set([...array1, ...array2])]
    
    console.log(union)

    0 讨论(0)
  • 2020-12-03 12:57

    Here is better solution

    function arrayUnion() {
        var args = Array.prototype.slice.call(arguments);
        var it = args.pop();
    
        return _.uniq(_.flatten(args, true), it);
    }
    
    var a = [{id: 0}, {id: 1}, {id: 2}];
    var b = [{id: 2}, {id: 3}];
    var c = [{id: 0}, {id: 1}, {id: 2}];
    
    var result = arrayUnion(a, b, c, function (item) {
        return item.id;
    });
    
    console.log(result); 
    
    0 讨论(0)
  • 2020-12-03 12:59

    You can do it using the underscore way:

    // collectionUnion(*arrays, iteratee)
    function collectionUnion() {
        var args = Array.prototype.slice.call(arguments);
        var it = args.pop();
    
        return _.uniq(_.flatten(args, true), it);
    }
    

    It just an improvment of the original function _.union(*arrays), adding an iteratee to work collection (array of object).

    Here how to use it:

    var result = collectionUnion(a, b, c, function (item) {
        return item.id;
    });
    

    The original function which is just working with array, looks like that:

    _.union = function() {
      return _.uniq(flatten(arguments, true, true));
    };
    

    And in bonus a full example:

    // collectionUnion(*arrays, iteratee)
    function collectionUnion() {
        var args = Array.prototype.slice.call(arguments);
        var it = args.pop();
    
        return _.uniq(_.flatten(args, true), it);
    }
    
    var a = [{id: 0}, {id: 1}, {id: 2}];
    var b = [{id: 2}, {id: 3}];
    var c = [{id: 0}, {id: 1}, {id: 2}];
    
    var result = collectionUnion(a, b, c, function (item) {
        return item.id;
    });
    
    console.log(result); // [ { id: 0 }, { id: 1 }, { id: 2 }, { id: 3 } ]
    
    0 讨论(0)
  • 2020-12-03 13:03

    You could implement your own pretty easily. In this case, we make the function generic, so that it can take arrays of any data type(s) and union them using the comparator function provided.

    // arr1 and arr2 are arrays of any length; equalityFunc is a function which
    // can compare two items and return true if they're equal and false otherwise
    function arrayUnion(arr1, arr2, equalityFunc) {
        var union = arr1.concat(arr2);
    
        for (var i = 0; i < union.length; i++) {
            for (var j = i+1; j < union.length; j++) {
                if (equalityFunc(union[i], union[j])) {
                    union.splice(j, 1);
                    j--;
                }
            }
        }
    
        return union;
    }
    
    function areGamesEqual(g1, g2) {
        return g1.title === g2.title;
    }
    
    // Function call example
    arrayUnion(arr1, arr2, areGamesEqual);
    

    Refer to Object comparison in JavaScript for various object comparison implementations.

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