1

I'm trying to make a unique list of all the values assigned to the city property, which is buried in an object within an object, and I have an array of such objects

buildings = [ { name: 'Victoria Bell Tower', filterOptions: { city: 'Ottowa', state: 'Canada', buildingType: 'Parliament Building', }, }, { name: 'Cathedral', filterOptions: { city: 'Washington D.C.', state: '', buildingType: 'Cathedral', }, }, { name: 'Post Office', filterOptions: { city: 'Washington D.C.', state: '', buildingType: 'Post Office', }, }]

What's a practical way to get a unique array of the cities properties:

cities = ['Ottowa', 'Washington D.C.']

Mugunga
  • 45
  • 4

1 Answers1

2

You can try my simple code:

let arr = buildings.map(b => {
  return b.filterOptions.city
})
console.log([...new Set(arr)]);
Ryan Nghiem
  • 2,417
  • 2
  • 18
  • 28