const array = [
{id:3, name: 'Central Microscopy', fiscalYear: 2018},
{id:5, name: 'Crystallography Facility', fiscalYear: 2018},
{id:3, name: 'Central Microscopy', fiscalYear: 2017},
{id:5, name: 'Crystallography Facility', fiscalYear: 2017},
];
// Array.from() method creates a new, shallow-copied Array instance from an array-like or iterable object.
// The find() method returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned.
var result = Array.from(new Set(array.map( s => s.id)))
.map( id => {
return {
id: id,
name: array.find(s => s.id === id).name
};
});
result
// Can also be done in one loop only as the above is not efficient for huge array
var result = [];
const map = new Map();
for (const item of array) {
if(!map.has(item.id)){
map.set(item.id, true); // set any value to Map
result.push({
id: item.id,
name: item.name
});
}
}
console.log(result)