Last active
September 25, 2018 23:28
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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