Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export function twoArraysIntersectionByProperties<T>(arr1: T[], arr2: T[], propertyNames: string[]): T[] {
  2.     const allElements = arr1.concat(...arr2);
  3.     if (!propertyNames.length || !arr1.length || !arr2.length) {
  4.         return allElements;
  5.     }
  6.     const uniqPropertiesNames = [...new Set(propertyNames)];
  7.     return arr1.filter((arr1Element) => {
  8.         return arr2.filter((arr2Element) => {
  9.             let hasIntersection = true;
  10.             uniqPropertiesNames.forEach((prop) => {
  11.                 if (arr1Element[prop] !== arr2Element[prop]) {
  12.                     hasIntersection = false;
  13.                 }
  14.             });
  15.             return hasIntersection;
  16.         }).length;
  17.     });
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement