Подтвердить что ты не робот

Разность и пересечение двух массивов, содержащих объекты

У меня есть два массива list1 и list2 которых есть объекты с некоторыми свойствами; userId - это идентификатор или уникальное свойство:

list1 = [
    { userId: 1234, userName: 'XYZ'  }, 
    { userId: 1235, userName: 'ABC'  }, 
    { userId: 1236, userName: 'IJKL' },
    { userId: 1237, userName: 'WXYZ' }, 
    { userId: 1238, userName: 'LMNO' }
]

list2 = [
    { userId: 1235, userName: 'ABC'  },  
    { userId: 1236, userName: 'IJKL' },
    { userId: 1252, userName: 'AAAA' }
]

Я ищу простой способ выполнить следующие три операции:

  1. list1 operation list2 должен возвращать пересечение элементов:

    [
        { userId: 1235, userName: 'ABC'  },
        { userId: 1236, userName: 'IJKL' }
    ]
    
  2. list1 operation list2 должен возвращать список всех элементов из list1 которые не встречаются в list2:

    [
        { userId: 1234, userName: 'XYZ'  },
        { userId: 1237, userName: 'WXYZ' }, 
        { userId: 1238, userName: 'LMNO' }
    ]
    
  3. list2 operation list1 должен возвращать список элементов из list2 которые не встречаются в list1:

    [
        { userId: 1252, userName: 'AAAA' }
    ]
    
4b9b3361

Ответ 1

Это решение, которое сработало для меня.

 var intersect = function (arr1, arr2) {
            var intersect = [];
            _.each(arr1, function (a) {
                _.each(arr2, function (b) {
                    if (compare(a, b))
                        intersect.push(a);
                });
            });

            return intersect;
        };

 var unintersect = function (arr1, arr2) {
            var unintersect = [];
            _.each(arr1, function (a) {
                var found = false;
                _.each(arr2, function (b) {
                    if (compare(a, b)) {
                        found = true;    
                    }
                });

                if (!found) {
                    unintersect.push(a);
                }
            });

            return unintersect;
        };

        function compare(a, b) {
            if (a.userId === b.userId)
                return true;
            else return false;
        }

Ответ 2

Вы можете определить три функции inBoth, inFirstOnly и inSecondOnly которые все берут два списка в качестве аргументов и возвращают список, который можно понять из имени функции. Основная логика может быть включена в общую функциональную operation которую полагаются все три.

Вот несколько вариантов реализации этой operation, для которых вы можете найти фрагмент ниже:

  • Обычный старый JavaScript for петель
  • Функции стрелок с использованием filter и some методов массива
  • Оптимизированный поиск с помощью Set

Обычный for петель

// Generic helper function that can be used for the three operations:        
function operation(list1, list2, isUnion) {
    var result = [];
    
    for (var i = 0; i < list1.length; i++) {
        var item1 = list1[i],
            found = false;
        for (var j = 0; j < list2.length && !found; j++) {
            found = item1.userId === list2[j].userId;
        }
        if (found === !!isUnion) { // isUnion is coerced to boolean
            result.push(item1);
        }
    }
    return result;
}

// Following functions are to be used:
function inBoth(list1, list2) {
    return operation(list1, list2, true);
}

function inFirstOnly(list1, list2) {
    return operation(list1, list2);
}

function inSecondOnly(list1, list2) {
    return inFirstOnly(list2, list1);
}

// Sample data
var list1 = [
    { userId: 1234, userName: 'XYZ'  }, 
    { userId: 1235, userName: 'ABC'  }, 
    { userId: 1236, userName: 'IJKL' },
    { userId: 1237, userName: 'WXYZ' }, 
    { userId: 1238, userName: 'LMNO' }
];
var list2 = [
    { userId: 1235, userName: 'ABC'  },  
    { userId: 1236, userName: 'IJKL' },
    { userId: 1252, userName: 'AAAA' }
];
  
console.log('inBoth:', inBoth(list1, list2)); 
console.log('inFirstOnly:', inFirstOnly(list1, list2)); 
console.log('inSecondOnly:', inSecondOnly(list1, list2)); 

Ответ 3

Используйте метод _.isEqual. В частности:

list1.reduce(function(prev, curr){
  !list2.some(function(obj){
    return _.isEqual(obj, curr)
  }) ? prev.push(curr): false;
  return prev
}, []);

Выше вы A given !B эквивалент A given !B (в терминах SQL, A LEFT OUTER JOIN B). Вы можете переместить код вокруг кода, чтобы получить то, что вы хотите!

Ответ 4

короткий ответ:

list1.filter(a => list2.some(b => a.userId === b.userId));  
list1.filter(a => !list2.some(b => a.userId === b.userId));  
list2.filter(a => !list1.some(b => a.userId === b.userId));  

более длинный ответ:
Приведенный выше код будет проверять объекты по значению userId,
если вам нужны сложные правила сравнения, вы можете определить собственный компаратор:

comparator = function (a, b) {
    return a.userId === b.userId && a.userName === b.userName
};  
list1.filter(a => list2.some(b => comparator(a, b)));
list1.filter(a => !list2.some(b => comparator(a, b)));
list2.filter(a => !list1.some(b => comparator(a, b)));

Также есть способ сравнить объекты по ссылкам
ПРЕДУПРЕЖДЕНИЕ! два объекта с одинаковыми значениями будут считаться разными:

o1 = {"userId":1};
o2 = {"userId":2};
o1_copy = {"userId":1};
o1_ref = o1;
[o1].filter(a => [o2].includes(a)).length; // 0
[o1].filter(a => [o1_copy].includes(a)).length; // 0
[o1].filter(a => [o1_ref].includes(a)).length; // 1

Ответ 5

function intersect(first, second) {
    return intersectInternal(first, second, function(e){ return e });
}

function unintersect(first, second){
    return intersectInternal(first, second, function(e){ return !e });  
}

function intersectInternal(first, second, filter) {
    var map = {};

    first.forEach(function(user) { map[user.userId] = user; });

    return second.filter(function(user){ return filter(map[user.userId]); })
}

Ответ 6

Вот функциональное программирующее решение с подчеркиванием /lodash для ответа на ваш первый вопрос (пересечение).

list1 = [ {userId:1234,userName:'XYZ'}, 
          {userId:1235,userName:'ABC'}, 
          {userId:1236,userName:'IJKL'},
          {userId:1237,userName:'WXYZ'}, 
          {userId:1238,userName:'LMNO'}
        ];

list2 = [ {userId:1235,userName:'ABC'},  
          {userId:1236,userName:'IJKL'},
          {userId:1252,userName:'AAAA'}
        ];

_.reduce(list1, function (memo, item) {
        var same = _.findWhere(list2, item);
        if (same && _.keys(same).length === _.keys(item).length) {
            memo.push(item);
        }
        return memo
    }, []);

Я позволю вам улучшить это, чтобы ответить на другие вопросы ;-)

Ответ 7

Просто используйте методы массива filter и some в JS, и вы можете это сделать.

let arr1 = list1.filter(e => {
   return !list2.some(item => item.userId === e.userId);
});

Это вернет элементы, которые присутствуют в list1, но отсутствуют в list2. Если вы ищете общие элементы в обоих списках. Просто сделай это.

let arr1 = list1.filter(e => {
   return list2.some(item => item.userId === e.userId); // take the ! out and you're done
});