Given two arrays of objects, how can I find all objects that contain an 'overlapping' value (e.g. price in this case)?
For example, given array A and array B, how can I find all objects where the "price" is either an exact match (e.g. 20, 30) or contained within this overlap (e.g. 20.45)?
var A = [
{ "id" : 1, "price" : 50, "quantity": 2 },
{ "id" : 2, "price" : 40, "quantity": 2 },
{ "id" : 3, "price" : 30, "quantity": 2 }, // yes
{ "id" : 4, "price" : 20, "quantity": 2 } // yes
];
var B = [
{ "id" : 5, "price" : 30, "quantity": 2 }, // yes
{ "id" : 6, "price" : 20.45, "quantity": 2 }, // yes
{ "id" : 7, "price" : 20, "quantity": 2 }, // yes
{ "id" : 8, "price" : 10, "quantity": 2 },
{ "id" : 9, "price" : 5, "quantity": 2 }
];
// Goal
var C = [
{ "id" : 3, "price" : 30, "quantity": 2 }, // yes
{ "id" : 4, "price" : 20, "quantity": 2 } // yes
];
var D = [
{ "id" : 5, "price" : 30, "quantity": 2 }, // yes
{ "id" : 6, "price" : 20.45, "quantity": 2 }, // yes
{ "id" : 7, "price" : 20, "quantity": 2 }, // yes
];
My goal is to keep them separated into their own arrays (C & D). But if the end result needs to be one combined array, that's okay. I can probably make that work too. Anything that works would make me happy right now.
I've tried Underscore's intersection. If A & B were simple arrays containing integers rather than objects, then intersection would work to find the exact matches (e.g. 30 & 30, 20 & 20), but it still wouldn't include 20.45, which I need as well. And, of course, I have an array of objects instead of simple arrays, which makes it a bit harder as well.