unique-objects
TypeScript icon, indicating that this package has built-in type declarations

1.0.5 • Public • Published

unique-objects

Build Status npm codecov Gzip Size

Get unique objects from array of objects based on single/multiple keys.

Install

$ npm i unique-objects

Usage

This can be helpful in cases where you need unique records based on a single key or multiple keys.

const uniqueObjects = require('unique-objects');
const users = [
    { id: 1, username: 'bret' },
    { id: 2, username: 'samantha' },
    { id: 2, username: 'samantha' },
    { id: 2, username: 'samantha2' },
    { id: 3, username: 'elvis' },
    { id: 3, username: 'elvis2' },
    { id: 3, username: 'elvis2' }
];
 
// Get unique objects in the users array based on just id
uniqueObjects(users, ['id']);
 
//=> Returns just 3 unique objects out of 7
// [
//  { "id": 1, "username": "bret"},
//  { "id": 2, "username": "samantha2"},
//  { "id": 3, "username": "elvis2"},
// ]
 
// Get unique objects in the users array based on both id & username
uniqueObjects(users, ['id', 'username']);
 
//=> Returns just 5 unique objects out of 7
// [
//  { "id": 1, "username": "bret"},
//  { "id": 2, "username": "samantha"},
//  { "id": 2, "username": "samantha2"},
//  { "id": 3, "username": "elvis"},
//  { "id": 3, "username": "elvis2"},
// ]

API

uniqueObjects(arrayInput, keys)

arrayInput

Type: Array
Default: []

Must be a JavaScript Array. This is main array of object that we are going to process.

keys

Type: Array
Default: []

Must be a JavaScript Array. This is the array of key(s) in the object that is used to find object is unique or not.

License

MIT © Palash Mondal

Package Sidebar

Install

npm i unique-objects

Weekly Downloads

11

Version

1.0.5

License

MIT

Unpacked Size

4.41 kB

Total Files

5

Last publish

Collaborators

  • palashmon