Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/palashmon/unique-objects
Get unique objects from an array of objects based on single/multiple keys.
https://github.com/palashmon/unique-objects
array array-item array-of-objects javascript-array json objects tiny-library unique
Last synced: about 1 month ago
JSON representation
Get unique objects from an array of objects based on single/multiple keys.
- Host: GitHub
- URL: https://github.com/palashmon/unique-objects
- Owner: palashmon
- License: mit
- Created: 2018-07-13T15:55:45.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-05-17T02:40:15.000Z (8 months ago)
- Last Synced: 2024-05-17T03:37:25.301Z (8 months ago)
- Topics: array, array-item, array-of-objects, javascript-array, json, objects, tiny-library, unique
- Language: JavaScript
- Homepage:
- Size: 84 KB
- Stars: 11
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# unique-objects ![CI](https://github.com/palashmon/unique-objects/actions/workflows/main.yml/badge.svg)
> 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.
```js
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](https://github.com/palashmon)