Mugunga
Mugunga

Reputation: 53

Javascript: How to get a unique list of values assigned to the same property from an array of objects?

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.']

Upvotes: 1

Views: 39

Answers (1)

Ryan Nghiem
Ryan Nghiem

Reputation: 2438

You can try my simple code:

let arr = buildings.map(b => {
  return b.filterOptions.city
})
console.log([...new Set(arr)]);

Upvotes: 2

Related Questions