Server : nginx/1.18.0 System : Linux localhost 6.14.3-x86_64-linode168 #1 SMP PREEMPT_DYNAMIC Mon Apr 21 19:47:55 EDT 2025 x86_64 User : www-data ( 33) PHP Version : 8.0.16 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, Directory : /var/www/ecommerce/node_modules/collect.js/docs/api/ |
# `unique()`
The unique method returns all of the unique items in the collection:
```js
const collection = collect([1, 1, 1, 2, 3, 3]);
const unique = collection.unique();
unique.all();
// [1, 2, 3]
```
When dealing with an array of objects, you may specify the key used to determine uniqueness:
```js
const collection = collect([
{ name: 'iPhone 6', brand: 'Apple', type: 'phone' },
{ name: 'iPhone 5', brand: 'Apple', type: 'phone' },
{ name: 'Apple Watch', brand: 'Apple', type: 'watch' },
{ name: 'Galaxy S6', brand: 'Samsung', type: 'phone' },
{ name: 'Galaxy Gear', brand: 'Samsung', type: 'watch' },
]);
const unique = collection.unique('brand');
unique.all();
// [
// { name: 'iPhone 6', brand: 'Apple', type: 'phone' },
// { name: 'Galaxy S6', brand: 'Samsung', type: 'phone' },
// ]
```
You may also pass your own callback to determine item uniqueness:
```js
const collection = collect([
{ name: 'iPhone 6', brand: 'Apple', type: 'phone' },
{ name: 'iPhone 5', brand: 'Apple', type: 'phone' },
{ name: 'Apple Watch', brand: 'Apple', type: 'watch' },
{ name: 'Galaxy S6', brand: 'Samsung', type: 'phone' },
{ name: 'Galaxy Gear', brand: 'Samsung', type: 'watch' },
]);
const unique = collection.unique(item => item.brand + item.type);
unique.all();
// [
// { name: 'iPhone 6', brand: 'Apple', type: 'phone' },
// { name: 'Apple Watch', brand: 'Apple', type: 'watch' },
// { name: 'Galaxy S6', brand: 'Samsung', type: 'phone' },
// { name: 'Galaxy Gear', brand: 'Samsung', type: 'watch' },
// ]
```
[View source on GitHub](https://github.com/ecrmnn/collect.js/blob/master/src/methods/unique.js)