🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

unique-objects

Package Overview
Dependencies
Maintainers
0
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

unique-objects

Get unique objects from array of objects

1.9.0
latest
Source
npm
Version published
Weekly downloads
439
-0.45%
Maintainers
0
Weekly downloads
 
Created
Source

unique-objects CI

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

Keywords

unique

FAQs

Package last updated on 01 Mar 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts