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

1 Answer 1

2

You can try my simple code:

let arr = buildings.map(b => {
  return b.filterOptions.city
})
console.log([...new Set(arr)]);
Sign up to request clarification or add additional context in comments.

4 Comments

This doesn't remove dups, though right? I need uniqe values for a dropdown
Do you try? This is a demo: codepen.io/phuongnm153/pen/oNgreZy
@Mugunga It would because of the use of Set which filters out duplicates
Ah, yes that works, thank you. Why does it need the [... ] around the new set instead of just: log(new Set(arr));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.