Hi,
How to write an equivalent simple Javascript method for '_.intersectionWith' method of LodashJs library ?
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
_.intersectionWith(objects, others, _.isEqual);
Output: [{ 'x': 1, 'y': 2 }]
Appreciate your help.
Here ya go:
function intersectionWith() { var comparator = arguments[arguments.length - 1]; var others = [].slice.call(arguments, 1, arguments.length - 1); var first = arguments[0]; var results = []; first.forEach(function(item) { if (results.find(function(result) { return comparator(item, result) })) { return; } if (others.every(function(other) { return other.find(function(otherItem) { return comparator(item, otherItem); }); })) { results.push(item); } }); return results; }And here it is in action with a shallow equal function: https://jsfiddle.net/shiftyp/7jevjxjr/