简体   繁体   English

如何在javascript中找到两个对象数组之间的所有交集?

[英]How to find all intersections between two arrays of objects in javascript React?

I have two arrays of objects. 我有两个对象数组。 The Arrays can be of any length. 阵列可以是任何长度的。 For example my first array "details" consists of three objects, 例如,我的第一个数组“细节”由三个对象组成,

let details = [
{a: 1, b: 200, name: "dad"}, 
{a:2, b: 250, name: "cat"}, 
{a:3, b: 212, name: "dog" } 
] 

second array consists of four objects: 第二个数组由四个对象组成:

let certs = [
{id: 991, b: 250, dn: "qwerty", sign: "GOST"}, 
{id: 950, b: 251, dn: "how", sign: "RSA" }, 
{id: 100, b: 250, dn: "how are", sign: "twofish" }, 
{id: 957, b: 212, dn: "how you", sign: "black" }
] 

How to obtain an array that consists of all intersections by some property of object, for example by 'b' ? 如何通过对象的某些属性获取由所有交叉点组成的数组,例如'b'?

I mean I want to obtain filtered array from "certs" array, that will contain only three objects not four. 我的意思是我想从“certs”数组中获取过滤数组,该数组只包含三个不是四个的对象。

Because element at index 1 in certs array has property "b" which equals to 251. But "details" array doesn't have object with property b equals to 251. 因为certs数组中索引1处的元素具有等于251的属性“b”。但是“details”数组没有具有属性b的对象等于251。

So my filtered certs array should consists of only three objects. 所以我的过滤后的certs数组应该只包含三个对象。 How to implement this? 怎么实现这个?

I tried lodash methods, but none of them fit. 我尝试了lodash方法,但它们都不合适。 For example: 例如:

_.intersectionBy(certs, details, 'b')

gives me only this: 只给我这个:

0: {id: 991, b: 250, dn: "qwerty", sign: "GOST"}
1: {id: 957, b: 212, dn: "how you", sign: "black"}
length: 2

This object doesnt exist in final array: 此对象在最终数组中不存在:

{id: 100, b: 250, dn: "how are", sign: "twofish" }

There is a handy library called lodash and it has a function called intersectionBy which really fits this problem. 有一个名为lodash的方便的库,它有一个名为intersectionBy的函数,它真的适合这个问题。

let details = [
{a: 1, b: 200, name: "dad"}, 
{a:2, b: 250, name: "cat"}, 
{a:3, b: 212, name: "dog" } 
] 
let certs = [
{id: 991, b: 250, dn: "qwerty", sign: "GOST"}, 
{id: 950, b: 251, dn: "how", sign: "RSA" }, 
{id: 100, b: 250, dn: "how are", sign: "twofish" }, 
{id: 957, b: 212, dn: "how you", sign: "black" }
] 
 _.intersectionBy(certs, details, 'b')

You could define a function that will accept an expression and evaluate it in order to filter according to your need. 您可以定义一个接受表达式并对其进行评估的函数,以便根据您的需要进行过滤。

Below example uses filter against your own expression to filter the set of items. 下面的示例对您自己的expression使用filter来过滤项目集。

 let details = [ {a: 1, b: 200, name: "dad"}, {a:2, b: 250, name: "cat"}, {a:3, b: 212, name: "dog" } ]; let certs = [ {id: 991, b: 250, dn: "qwerty", sign: "GOST"}, {id: 950, b: 251, dn: "how", sign: "RSA" }, {id: 100, b: 250, dn: "how are", sign: "twofish" }, {id: 957, b: 212, dn: "how you", sign: "black" } ]; const intersectByExpression = (a, b, expression) => { return [...a,...b].filter(i => expression(i)); // <-- evaluates the expression. // ^---------^-- concat items and filters. } // All elements where `b` exists and is exactly 250. console.log( intersectByExpression(details, certs, o => ob === 250) // ^--------------^-- Expression to evaluate. ); // Other samples. // All elements where id is lower or equals 100. console.log( intersectByExpression(details, certs, o => o.id <= 100) ); // All elements where id is greater than 950 and b is greater or equals 250. console.log( intersectByExpression(details, certs, o => o.id >= 950 && ob >= 250) ); 

let details = [
{a: 1, b: 200, name: "dad"}, 
{a:2, b: 250, name: "cat"}, 
{a:3, b: 212, name: "dog" } 
];

let certs = [
{id: 991, b: 250, dn: "qwerty", sign: "GOST"}, 
{id: 950, b: 251, dn: "how", sign: "RSA" }, 
{id: 100, b: 250, dn: "how are", sign: "twofish" }, 
{id: 957, b: 212, dn: "how you", sign: "black" }
]

const result = certs.filter(cert => details.find(detail => detail.b === cert.b));

console.log(result);

It would be great if you explain your problem with more examples, :) 如果你用更多的例子解释你的问题会很棒,:)

Simply use the filter() method twice: 只需使用filter()方法两次:

 let details = [ {a: 1, b: 200, name: "dad"}, {a:2, b: 250, name: "cat"}, {a:3, b: 212, name: "dog" } ] let certs = [ {id: 991, b: 250, dn: "qwerty", sign: "GOST"}, {id: 950, b: 251, dn: "how", sign: "RSA" }, {id: 100, b: 250, dn: "how are", sign: "twofish" }, {id: 957, b: 212, dn: "how you", sign: "black" } ] const result = certs.filter(cert => { let arr = details.filter(detail => detail.b === cert.b) return !(arr.length === 0) }); console.log(result) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM