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

    Function groupJoin

    • Correlates each element of the sequence with a collection of matching elements from another sequence.

      Type Parameters

      • TElement

        Type of elements within the outer sequence.

      • TInner

        Type of elements within the inner sequence.

      • TKey

        Type of key produced by the key selectors.

      • TResult

        Type of element returned by resultSelector.

      Parameters

      • source: Iterable<TElement>

        The outer sequence.

      • innerEnumerable: Iterable<TInner>

        Sequence whose elements are grouped and joined with the outer elements.

      • outerKeySelector: Selector<TElement, TKey>

        Selector that extracts the join key from each outer element.

      • innerKeySelector: Selector<TInner, TKey>

        Selector that extracts the join key from each inner element.

      • resultSelector: JoinSelector<TElement, IEnumerable<TInner>, TResult>

        Projection that combines an outer element with an IEnumerable of matching inner elements.

      • OptionalkeyComparator: EqualityComparator<TKey, TKey>

        Optional equality comparator used to match keys. Defaults to the library's standard equality comparison.

      Returns IEnumerable<TResult>

      A sequence produced by applying resultSelector to each outer element and its matching inner elements.

      The inner sequence is enumerated once to build an in-memory lookup before outer elements are processed. Each outer element is then evaluated lazily and preserves the original outer ordering.

      const categories = [
      { id: 1, name: 'Fruit' },
      { id: 2, name: 'Vegetable' },
      ];
      const products = [
      { name: 'Apple', categoryId: 1 },
      { name: 'Banana', categoryId: 1 },
      { name: 'Carrot', categoryId: 2 },
      ];

      const joined = groupJoin(
      categories,
      products,
      c => c.id,
      p => p.categoryId,
      (c, ps) => ({ ...c, products: ps.toArray() })
      ).toArray();

      console.log(joined);
      // [
      // { id: 1, name: 'Fruit', products: [ { name: 'Apple', categoryId: 1 }, { name: 'Banana', categoryId: 1 } ] },
      // { id: 2, name: 'Vegetable', products: [ { name: 'Carrot', categoryId: 2 } ] }
      // ]