Horje
filter array with unique objects javascript Code Example
filter array with unique objects javascript
const array =
  [
    { "name": "Joe", "age": 17 },
    { "name": "Bob", "age": 17 },
    { "name": "Carl", "age": 35 }
  ]

const key = 'age';

const arrayUniqueByKey = [...new Map(array.map(item =>
  [item[key], item])).values()];

console.log(arrayUniqueByKey);

   /*OUTPUT
       [
        { "name": "Bob", "age": 17 },
        { "name": "Carl", "age": 35 }
       ]
   */

 // Note: this will pick the last duplicated item in the list.
get unique values from array of objects javascript
const data = [
  { group: 'A', name: 'SD' }, 
  { group: 'B', name: 'FI' }, 
  { group: 'A', name: 'MM' },
  { group: 'B', name: 'CO'}
];
const unique = [...new Set(data.map(item => item.group))]; // [ 'A', 'B']
javascript map return array with distinc values
//ES6
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]
javascript get unique values from key
const unique = [...new Set(array.map(item => item.age))];




Javascript

Related
unique objects in array javascript Code Example unique objects in array javascript Code Example
textarea react native Code Example textarea react native Code Example
NullInjectorError: No provider for HttpClient! Code Example NullInjectorError: No provider for HttpClient! Code Example
angular material change placeholder color Code Example angular material change placeholder color Code Example
react check if focused Code Example react check if focused Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
9