简体   繁体   English

Javascript:如何从对象数组中获取分配给同一属性的唯一值列表?

[英]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.

相关问题 如何使用 javascript 从对象数组中执行添加相同键的对象值并返回唯一对象? - How to perform addition of same keys' values of objects and return unique objects from array of objects using javascript? Javascript 如何从对象数组中获取属性的所有值? - Javascript How to get all values for a property from an Array of Objects? 如何从对象列表中获取唯一值? - how to get unique values from a list of objects? 如何从对象数组中获取属性本身的所有唯一值,该属性本身就是数组 - How to get from an array of objects all unique values of a property that is an array itself JavaScript:从一组对象中获取唯一值及其计数? - JavaScript: Get unique values and their counts from an array of objects? 如何从对象数组中获取具有特定键的唯一值的对象? - How to get objects with unique values for specific keys from array of objects? 如何从javascript中的对象数组中获取唯一对象 - How to get unique objects from objects array in javascript 如何在对象数组中获取具有唯一值的键? (JavaScript) - How can I get keys with unique values in an array of objects? (JavaScript) 如何从 javascript 中的列表中获取唯一对象列表 - How to get list of unique objects from list in javascript 如何从对象数组中的 arrays 获取唯一值数组? - How to get array of unique values from arrays within an array of objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM