@dtjv/uniq-array-by
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

uniq-array-by

Create an array of unique values by object property.

This package is ESM only.

Install

$ npm install @dtjv/uniq-array-by

Usage

import uniqArrayBy from '@dtjv/uniq-array-by'

// unique values by nested property
uniqArrayBy(
  [
    { a: { b: 1, c: 0 } },
    { a: { b: 2, c: 1 } },
    { a: { b: 1, c: 2 } },
    { a: { b: 2, c: 1 } },
  ],
  'a.b'
)
//-> [ { a: { b: 1, c: 2 } }, { a: { b: 2, c: 1 } } ]

// unique values by callback
uniqArrayBy(
  [
    { a: { b: 1, c: 0 } },
    { a: { b: 2, c: 1 } },
    { a: { b: 1, c: 2 } },
    { a: { b: 2, c: 1 } },
  ],
  (item) => item.a.b
)
//-> [ { a: { b: 1, c: 2 } }, { a: { b: 2, c: 1 } } ]

API

uniqArrayBy(array, property?)

Returns a new array of unique values from given array.

array

Type: unknown[]
Default: []

An array of values.

property

Type: string | Function
Default: (v) => v

The property of an array item.

A string can be a dot path to a nested object property.

A function is called on each item of array to generate uniqueness of item. It is invoked with one argument - an array item. The order of result values is determined by the order they occur in array.

Author

License

MIT License

Package Sidebar

Install

npm i @dtjv/uniq-array-by

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

3.93 kB

Total Files

4

Last publish

Collaborators

  • dtjv