typescript distinct array of objects code example
Example 1: javascript map return array with distinc values
let array = [
{ "name": "Joe", "age": 17 },
{ "name": "Bob", "age": 17 },
{ "name": "Carl", "age": 35 }
];
array.map(item => item.age)
.filter((value, index, self) => self.indexOf(value) === index)
> [17, 35]
Example 2: javascript get unique values from key
const unique = [...new Set(array.map(item => item.age))];
Example 3: Return Distinct Array Object
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 } ];const result = [];const map = new Map();for (const item of array) { if(!map.has(item.id)){ map.set(item.id, true);