0

Is there any easier (ES6 preferably) way of getting the unique collection of expense types than below ?

const expenseObjList = [
{id:1, type:"books", title:"print book purchase", amount:50},
{id:2, type:"books", title:"ebook purchase", amount:10},
{id:3, type:"grocery", title:"vegetables", amount:5},
{id:4, type:"grocery", title:"milk", amount:5},
{id:5, type:"grocery", title:"eggs", amount:10}
]


const newArr = expenseObjList.map((item)=>{
  return item.type
})

const newSet = new Set(newArr)

console.log('set',newSet)

output

  • 1
    Not really. Best you could do is change to concise return with the arrow function - and if you don't use the `newArr` for anything else, feel free to remove it entirely and just pass the map result to `new Set`. `new Set(expenseObjList.map(item => item.type))`. (but if you do use the array for something else, you'll have to put it into a variable) – CertainPerformance Oct 12 '22 at 05:01

0 Answers0