Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/writetome51/array-get-unique-items
Returns every item in array without any duplicates
https://github.com/writetome51/array-get-unique-items
array itemsjs typescript unique
Last synced: 18 days ago
JSON representation
Returns every item in array without any duplicates
- Host: GitHub
- URL: https://github.com/writetome51/array-get-unique-items
- Owner: writetome51
- License: mit
- Created: 2018-10-01T03:38:25.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-09-08T20:32:08.000Z (over 4 years ago)
- Last Synced: 2024-12-09T04:26:54.834Z (about 1 month ago)
- Topics: array, itemsjs, typescript, unique
- Language: TypeScript
- Homepage:
- Size: 26.4 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# getUniqueItems(array): any[]
Returns every item in `array` without any duplicates. Does not modify `array`.
Note: duplicate arrays do not have to match using the `===` operator.
However, duplicate objects do. See the examples.## Examples
view examples
```
getUniqueItems( [ '', 1, 2, 3, '', 1, 2, 3, '' ] );
// --> [ '', 1, 2, 3 ]getUniqueItems( [ true, true, false, true, false, 'aa' ] );
// --> [ true, false, 'aa' ]// Objects appearing identical, but not identical in memory, are not
// considered duplicates:let obj1 = {name: 'steve'};
let obj2 = {name: 'steve'};getUniqueItems([obj1, obj2]);
// --> [ {name: 'steve'}, {name: 'steve'} ]// Try this:
let obj1 = {name: 'steve'};
let obj2 = obj1;getUniqueItems([obj1, obj2]);
// --> [ {name: 'steve'} ]// Arrays appearing identical, but not identical in memory, can still
// be considered duplicates. This is because the algorithm loops thru
// them, checking if array1[i] === array2[i]. If array1[i] and array2[i]
// are both arrays of identical length, the algorithm recursively operates
// on those arrays.getUniqueItems( [ [[1,2,3]], [[1,2,3]] ] );
// --> [ [[1,2,3]] ]// But, if the arrays contain objects not identical in memory, they're not
// considered duplicates:let arr = [ [[{name: 'steve'}]], [[{name: 'steve'}]] ];
getUniqueItems(arr);
// --> [ [[{name: 'steve'}]], [[{name: 'steve'}]] ]// Let's try that again with objects identical in memory:
let obj1 = {name: 'steve'};
let obj2 = obj1;
let arr = [ [[obj1]], [[obj2]] ];
getUniqueItems(arr);
// --> [ [[{name: 'steve'}]] ]
```## Installation
`npm i @writetome51/array-get-unique-items`## Loading
```js
import {getUniqueItems} from '@writetome51/array-get-unique-items';
```