Skip to content

Instantly share code, notes, and snippets.

@santiago-puchginer-snkeos
Last active September 25, 2018 23:28
Show Gist options
  • Save santiago-puchginer-snkeos/f9f4a222436c951c1eb4d4a9e26f365f to your computer and use it in GitHub Desktop.
Save santiago-puchginer-snkeos/f9f4a222436c951c1eb4d4a9e26f365f to your computer and use it in GitHub Desktop.
Filter an array of objects to have "unique" objects with respect some of their properties. Uses new Map from ES6.
// Data
let data = [
{
name: 'John',
SSID: 1
},
{
name: 'John',
SSID: 2
},
{
name: 'Doe',
SSID: 1
},
{
name: 'James',
SSID: 1
},
{
name: 'Clerk',
SSID: 2
},
{
name: 'Maxwell',
SSID: 1
},
{
name: 'Maxwell',
SSID: 2
},
];
// Print original data
console.log(data);
// Use Map (ES6) to retain only unique elements
let map = new Map();
for (element of data) {
map.set(element.name, element);
}
var filteredData = [];
map.forEach( (value, key, map) => {
filteredData.push(value);
});
// Print filtered data
console.log(filteredData);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment