When working with arrays of objects in JavaScript, the Underscore library's functions for array intersection, difference, and union may not work as expected. For example:
var first = {val: 1};
var otherFirst = {val: 1};
var second = {val: 2};
_.difference([first, second], [otherFirst]); // result is [first, second] instead of [second]
This discrepancy occurs because JavaScript compares objects based on reference equality. So how can two arrays of objects be intersected?
I am seeking an idiomatic solution to this issue.