仅某些字段 underscore.js 的对象数组之间的交集和等于

Intersection and Equals between arrays of objects by only some fields with underscore.js

我在我的项目中使用 underscore.js,我想比较两个数组,但只比较它们在一个字段中的交集。

第一个数组:

[{item: myObject1, label: myObject1.name, ticked: true, disabled: false}]

第二个数组:

[{item: myObject1, label: myObject1.name, ticked: false, disabled: false}, 
{item: myObject2, label: myObject2.name, ticked: true, disabled: false}]

我想 return 这些数组与 myObject.id 的交集并进行比较:

  1. 路口:

    {item: myObject1, label: myObject1.name, ticked: true, disabled: false}
    

{item: myObject1, label: myObject1.name, ticked: false, disabled: false}

因为 myObject1 是交集 (myObject1.id == myObject1.id).

  1. 这些项目不相等,因为第一个元素被打勾而第二个没有...所以,return false;

我不知道如何用 underscore.js 做到这一点。

编辑:

只有 isEqual 函数,我可以做到(我使用 AngularJS):

isIOEquals: function(inputs, outputs){
        var editedInputs = [];
        var editedOutputs = [];
        angular.forEach(outputs, function(outputItem){
            angular.forEach(inputs, function(inputItem){
                if(_.isEqual(inputItem.item.id, outputItem.item.id)){
                    editedInputs.push(inputItem);
                    editedOutputs.push(outputItem);
                }
            });
        });

        return _.isEqual(editedInputs, editedOutputs);
    }

我相信这会成功。

function myIntersection() {
    var objectLists = [].slice.call(arguments),
        objMapper = objectLists.pop();

    var flatLists = objectLists.map(function(lst) {
        return lst.map(objMapper);
    });

    // There will be duplicates in this list, but maybe that's the desired
    // behaviour, so I'll leave it to you to sort that out.
    var mergedObjectLists = _.flatten(objectLists);

    var intersectingValues = _.intersection.apply(null, flatLists);
    return mergedObjectLists.filter(function(obj) {
        return intersectingValues.indexOf(objMapper(obj)) !== -1
    });
}

myIntersection(list1, list2, function(obj) {return obj.item.id});

我很乐意看到其他方法。