Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export function twoArraysIntersectionByProperties<T>(arr1: T[], arr2: T[], propertyNames: string[]): T[] {
- const allElements = arr1.concat(...arr2);
- if (!propertyNames.length || !arr1.length || !arr2.length) {
- return allElements;
- }
- const uniqPropertiesNames = [...new Set(propertyNames)];
- return arr1.filter((arr1Element) => {
- return arr2.filter((arr2Element) => {
- let hasIntersection = true;
- uniqPropertiesNames.forEach((prop) => {
- if (arr1Element[prop] !== arr2Element[prop]) {
- hasIntersection = false;
- }
- });
- return hasIntersection;
- }).length;
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement