get-unique-objects
v1.0.1
Published
Get unique objects from array of objects
Downloads
2
Readme
Get Unique Objects
Code Quality Status
Description
Get unique objects from array of objects (can be normalize before compare them)
Installation
npm i get-unique-objects
Params
getUniqueObjects(items, options)
items
- Items to be filter
- Type:
Array
ofObject
- Required
Example
// Example - 1
[]
// Example - 2
[
{ name: 'Buenos Aires', country: 'Argentina'}
]
// Example - 3
[
{ name: 'Buenos Aires', country: 'Argentina'},
{ name: 'Villa Carlos Paz', country: 'Argentina', province: 'Córdoba' },
]
options
- Options to normalize the objects before compare
- Type:
Object
- Optional
Example
{
fieldsToKeep: ["name", "number"]
}
:link: See more in Object Normalize
Return
getUniqueObjects(items, options) : uniqueItems
- Items not repeated after normalize them, unique Items
- Type:
Array
ofObject
Usage
getUniqueObjects([items])
Filter unique objects from items
const getUniqueObjects = require('get-unique-objects');
const playerSample1 = {
name: "Juan Román",
lastname: "Riquelme",
clubs: ["Boca", "Barcelona", "Villareal", "Argentinos"],
number: 10,
stillPlaying: false,
birthPlace: { country: "Argentina", province: "Buenos Aires", city: "Don Torcuato" }
};
const playerSample2 = {
name: "Juan Román",
lastname: "Riquelme",
clubs: ["Boca", "Barcelona", "Villareal", "Argentinos"],
number: 10
}
getUniqueObjects([playerSample1, playerSample2]);
/*
output: [
{
name: "Juan Román",
lastname: "Riquelme",
clubs: ["Boca", "Barcelona", "Villareal", "Argentinos"],
number: 10,
stillPlaying: false,
birthPlace: { country: "Argentina", province: "Buenos Aires", city: "Don Torcuato" }
},
{
name: "Juan Román",
lastname: "Riquelme",
clubs: ["Boca", "Barcelona", "Villareal", "Argentinos"],
number: 10
}
]
*/
getUniqueObjects([playerSample1, playerSample1]);
/*
output: [
{
name: "Juan Román",
lastname: "Riquelme",
clubs: ["Boca", "Barcelona", "Villareal", "Argentinos"],
number: 10,
stillPlaying: false,
birthPlace: { country: "Argentina", province: "Buenos Aires", city: "Don Torcuato" }
}
]
*/
getUniqueObjects([]);
/*
output: []
*/
getUniqueObjects([items], options)
Filter unique objects from items after normalize them
const getUniqueObjects = require('get-unique-objects');
const playerSample1 = {
name: "Juan Román",
lastname: "Riquelme",
clubs: ["Boca", "Barcelona", "Villareal", "Argentinos"],
number: 10,
stillPlaying: false,
birthPlace: { country: "Argentina", province: "Buenos Aires", city: "Don Torcuato" }
};
const playerSample2 = {
name: "Juan Román",
lastname: "Riquelme",
clubs: ["Boca", "Barcelona", "Villareal", "Argentinos"],
number: 10
}
getUniqueObjects([playerSample1, playerSample2], { fieldsToRemove: ["number", "stillPlaying", "birthPlace"] });
/*
output: [
{
name: "Juan Román",
lastname: "Riquelme",
clubs: ["Boca", "Barcelona", "Villareal", "Argentinos"]
}
]
*/
const playerSample3 = {
name: "Lionel",
lastname: "Messi",
clubs: ["Barcelona"],
number: 10,
stillPlaying: true
}
getUniqueObjects([playerSample1, playerSample2, playerSample3], { fieldsToKeep: ["name", "lastname"]});
/*
output: [
{
name: "Juan Román",
lastname: "Riquelme"
},
{
name: "Lionel",
lastname: "Messi"
}
]
*/
getUniqueObjects([playerSample1, playerSample2, playerSample3], { fieldsToKeep: ["number"]});
/*
output: [
{
number: 10
}
]
*/