How to get distinct values from an array of objects in JavaScript

今日有個 task 其中一個地方要在 Array of Json object 中
Distinct 一個 property

const data = [
  {
    id: 1,
    category: 'fruit',
    name: 'Apple',
  },
  {
    id: 2,
    category: 'fruit',
    name: 'Banana',
  },
  {
    id: 3,
    category: 'herb',
    name: 'Basil',
  },
  {
    id: 4,
    category: 'herb',
    name: 'Rosemary',
  },
  {
    id: 5,
    category: 'fruit',
    name: 'Dragon Fruit',
  },
];

解決方法: (ES5)

 var categories = data.map( // try to retrieve specific property
        function(item) {
          return item.category;
        }) // Distinct
        .filter(function(item, index, arr){
          return arr.indexOf(item) === index;
        }) // sort
        .sort();

解決方法: (ES6)

 const categories = [...new Set(data.map(x => x.category ))];

Hope you find it useful

Chi Yau
Chi Yau
Software Engineer

Interested on Learning new stuff and experience sharing