如何从JavaScript中的对象数组中找到“重叠”值?(How to find 'overlapping' values from an array of objects in

编程入门 行业动态 更新时间:2024-10-11 23:22:31
如何从JavaScript中的对象数组中找到“重叠”值?(How to find 'overlapping' values from an array of objects in JavaScript?)

给定两个对象数组,如何找到包含“重叠”值的所有对象(例如本例中的价格)?

例如,给定数组A和数组B,我如何找到“价格”是完全匹配(例如20,30)或包含在此重叠中的所有对象(例如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 ];

我的目标是将它们分成各自的数组(C&D)。 但如果最终结果需要是一个组合数组,那没关系。 我也可以做那个工作。 任何有效的东西都会让我很开心。

我尝试过Underscore的交集。 如果A&B是包含整数而不是对象的简单数组,那么交集会找到完全匹配(例如30&30,20和20),但它仍然不包括20.45,我也需要它。 当然,我有一个对象数组而不是简单的数组,这使得它更难一点。

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.

最满意答案

似乎我们可以假设这些数组按价格排序(如果没有,请执行此操作 - 请参阅排序JavaScript对象和类似数组 ),并且A包含的价格高于B (如果不是,则需要反转逻辑)。 然后就做:

var i = A.length - 1, j = 0, overlapMin = A[i].price, overlapMax = B[j].price; while (A[i].price <= overlapMax) i--; while (B[j].price >= overlapMin) j++; var C = A.slice(i+1), // to end D = B.slice(0, j);

如果你需要在一个非常庞大的集合上获得更好的性能,你也可以对边界i和j使用二进制搜索。

It seems we can assume that those arrays are sorted by price (if not, do so - see Sorting an array of JavaScript objects and similar), and that A contains higher prices than B (if not, you'd need to invert the logic). Then just do:

var i = A.length - 1, j = 0, overlapMin = A[i].price, overlapMax = B[j].price; while (A[i].price <= overlapMax) i--; while (B[j].price >= overlapMin) j++; var C = A.slice(i+1), // to end D = B.slice(0, j);

If you need better performance on a really huge set, you could use binary searches for the boundaries i and j as well.

更多推荐

本文发布于:2023-08-03 09:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1385382.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   对象   中找到   JavaScript   find

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!