Skip to content

writetome51/array-get-unique-items

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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';

About

Returns every item in array without any duplicates

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published