@writetome51/array-get-unique-items
TypeScript icon, indicating that this package has built-in type declarations

2.0.2 • Public • Published

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

import {getUniqueItems} from '@writetome51/array-get-unique-items';

Package Sidebar

Install

npm i @writetome51/array-get-unique-items

Weekly Downloads

10

Version

2.0.2

License

MIT

Unpacked Size

4.13 kB

Total Files

5

Last publish

Collaborators

  • writetome51