Answers for "javascript filter array of objects unique"

8

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.
Posted by: Guest on September-14-2020
1

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]
Posted by: Guest on July-18-2020
3

javascript get unique values from key

const unique = [...new Set(array.map(item => item.age))];
Posted by: Guest on June-05-2020

Code answers related to "javascript filter array of objects unique"

Code answers related to "Javascript"

Browse Popular Code Answers by Language