【发布时间】:2021-01-04 15:16:21
【问题描述】:
说,我有一个包含国家、州、城市的对象数组。我想在这个数组上使用 .map 来根据我的要求获得唯一的国家或州或城市。我该如何编写一个方法接受一个列名,我想映射并相应地返回结果。
GetUniqueValues(colName) { // colName could be county or state
const uniqueVals = Array.from(new Set(this.myArray.map(item => item.country))); // how do I include colName here without writing item.country or item.state as I have many other fields to map
return uniqueVals;
}
【问题讨论】:
标签: javascript typescript