@mirei/ts-collections
    Preparing search index...

    Function intersectBy

    • Returns the elements whose keys are common to source and other.

      Type Parameters

      • TElement

        Type of elements within the source iterable.

      • TKey

        Type of key produced by keySelector.

      Parameters

      • source: Iterable<TElement>

        The source iterable.

      • other: Iterable<TElement>

        Iterable whose elements define the keys considered part of the intersection.

      • keySelector: Selector<TElement, TKey>

        Selector used to project each element to the key used for comparison.

      • OptionalkeyComparator: EqualityComparator<TKey, TKey> | OrderComparator<TKey, TKey>

        Optional comparator used to compare keys. Both equality and order comparators are supported; defaults to the library's standard equality comparison when omitted.

      Returns IEnumerable<TElement>

      A sequence containing the intersection of the two sequences based on matching keys.

      other is fully enumerated to materialise the inclusion keys before yielding results. Source ordering is preserved.

      const products1 = [
      { name: 'Apple', category: 'Fruit' },
      { name: 'Carrot', category: 'Vegetable' },
      ];
      const products2 = [
      { name: 'Banana', category: 'Fruit' },
      { name: 'Broccoli', category: 'Vegetable' },
      ];

      const result = intersectBy(products1, products2, p => p.category).toArray();
      console.log(result);
      // [
      // { name: 'Apple', category: 'Fruit' },
      // { name: 'Carrot', category: 'Vegetable' }
      // ]