https://github.com/dimfeld/unique-by
https://github.com/dimfeld/unique-by
Last synced: over 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/dimfeld/unique-by
- Owner: dimfeld
- License: mit
- Created: 2021-02-02T23:16:32.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-02-03T02:25:24.000Z (over 5 years ago)
- Last Synced: 2025-03-11T14:27:32.336Z (over 1 year ago)
- Language: JavaScript
- Size: 6.84 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# unique-by
Filter an array of items based on a function that returns a value. The resulting array will have only one item for each value returned by the callback.
```js
import uniqueBy from '@dimfeld/unique-by';
let items = [
{ first: 'Bob', last: 'Johnson' },
{ first: 'Alice', last: 'Rama' },
{ first: 'Bob', last: 'Bobber' },
{ first: 'Xavier', last: 'Xylophone' },
{ first: 'Bob', last: 'Johnson' },
{ first: 'George', last: 'Costanza' },
{ first: 'Xavier', last: 'Xylophone' },
{ first: 'Bob', last: 'Bobber' },
{ first: 'Sally', last: 'Sappa' },
{ first: 'Alice', last: 'Rama' },
{ first: 'Sarah', last: 'Dara' },
{ first: 'Sarah', last: 'Dara' },
{ first: 'John', last: 'Bob' },
];
let filtered = uniqueBy(items, (i) => i.first + i.last);
console.dir(items);
// [
// { first: 'Bob', last: 'Johnson' },
// { first: 'Alice', last: 'Rama' },
// { first: 'Bob', last: 'Bobber' },
// { first: 'Xavier', last: 'Xylophone' },
// { first: 'George', last: 'Costanza' },
// { first: 'Sally', last: 'Sappa' },
// { first: 'Sarah', last: 'Dara' },
// { first: 'John', last: 'Bob' },
// ];
```