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

    Function distinctBy

    • Eliminates duplicate elements by comparing keys computed for each element.

      Type Parameters

      • TElement

        Type of elements within the source iterable.

      • TKey

        Key type returned by keySelector.

      Parameters

      • source: Iterable<TElement>

        The source iterable.

      • keySelector: Selector<TElement, TKey>

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

      • OptionalkeyComparator: EqualityComparator<TKey, TKey>

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

      Returns IEnumerable<TElement>

      A sequence that contains the first occurrence of each unique key.

      Each element's key is evaluated exactly once; cache expensive key computations when possible.

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

      const distinctByCategory = distinctBy(products, p => p.category).toArray();
      console.log(distinctByCategory);
      // [
      // { name: 'Apple', category: 'Fruit' },
      // { name: 'Carrot', category: 'Vegetable' }
      // ]