JSFiddle - React, Tailwind, and code Playground
by Luis Perez
JavaScript
// _.intersectionObjects() for the intersection of objects
// using deep value comparison instead of reference comparison
// for objects
_.intersectionObjects = _.intersect = function(array) {
var slice = Array.prototype.slice;
var rest = slice.call(arguments, 1);
return _.filter(_.uniq(array), function(item) {
return _.every(rest, function(other) {
//return _.indexOf(other, item) >= 0;
return _.any(other, function(element) { return _.isEqual(element, item); });
});
});
};
var a = [ {'id': 1, 'name': 'jake' }, {'id':4, 'name': 'jenny'} ];
var b = [ {'id': 1, 'name': 'jake' }, {'id': 9, 'name': 'nick'} ];
var result = _.intersectionObjects(a, b);
document.write("result:" + JSON.stringify(result));