TS-Scribe - v0.6.2
    Preparing search index...

    Function uniqueArrayBy

    • Returns a new array with only the unique elements based on a key extracted from each element.

      The function uses the provided key extractor function to determine uniqueness of each element. If an element's key has been encountered before, it will be excluded from the result.

      Type Parameters

      • T
      • K

      Parameters

      • array: T[]

        The array to filter for unique elements.

      • keyFunc: KeyExtractor<T, K>

        A function that extracts the key from each element to determine uniqueness.

      Returns T[]

      A new array with only the unique elements based on the key.

      const arr = [
      { id: 1, name: 'Alice' },
      { id: 2, name: 'Bob' },
      { id: 1, name: 'Alice' }
      ];

      const uniqueArr = uniqueArrayBy(arr, item => item.id);
      console.log(uniqueArr);
      // Output: [
      // { id: 1, name: 'Alice' },
      // { id: 2, name: 'Bob' }
      // ]