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

    Function unionBy

    • Creates a set-style union between source and other by comparing keys projected from each element.

      Type Parameters

      • TElement

        Type of elements contained by the input sequences.

      • TKey

        Type of key produced by keySelector.

      Parameters

      • source: Iterable<TElement>

        The initial sequence whose distinct elements lead the union.

      • other: Iterable<TElement>

        Additional sequence whose elements are appended after source when forming the union.

      • keySelector: Selector<TElement, TKey>

        Projection that produces a comparison key for each element.

      • Optionalcomparator: EqualityComparator<TKey, TKey>

        Optional equality comparator that determines whether two keys are considered the same. Defaults to the library's standard equality comparator.

      Returns IEnumerable<TElement>

      A deferred sequence containing the distinct elements from source followed by elements from other whose keys were not previously observed.

      Re-throws any error thrown while iterating either sequence or executing keySelector or comparator.

      Keys are buffered to ensure uniqueness while elements remain lazily produced. Provide comparator when keys require structural equality semantics.

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

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