[英]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我正在尝试为分配给 city 属性的所有值创建一个唯一列表,该列表被埋在一个对象中的一个对象中,并且我有一个此类对象的数组
buildings = [ { name: 'Victoria Bell Tower', filterOptions: { city: 'Ottowa', state: 'Canada', buildingType: 'Parliament Building', }, }, { name: 'Cathedral', filterOptions: { city: 'Washington DC', state: '', buildingType: 'Cathedral', }, }, { name: 'Post Office', filterOptions: { city: 'Washington DC', state: '', buildingType: 'Post Office', }, }]
What's a practical way to get a unique array of the cities properties:获得独特的城市属性数组的实用方法是什么:
cities = ['Ottowa', 'Washington DC']
You can try my simple code:你可以试试我的简单代码:
let arr = buildings.map(b => {
return b.filterOptions.city
})
console.log([...new Set(arr)]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.