我有以下对象列表:
var data = [
{
"AcctId": 100,
"Value": true
},
{
"AcctId": 200,
"Value": false
}
]
我想像这样选择不同的 AcctId:
Output: [100, 200]
我试过使用 Angular 过滤法:
var newData = data.filter(r => r.AcctId);
但是,它会返回与输入相同的列表。谁能指导我做错了什么?
像这样尝试:
Working Demo
this.result = Array.from(new Set(this.data.map(x => x.acctId)));
或者,
constructor() {
this.result = this.GetDistinctValues(this.data, "acctId").map(x => x.acctId);
}
GetDistinctValues(Source: Array<any>, FilterKey: string = null): Array<any> {
let DistinctArray = [];
try {
Source.forEach(e => {
if (FilterKey !== null && FilterKey !== undefined && FilterKey !== "") {
if (
DistinctArray.filter(DE => DE[FilterKey] === e[FilterKey]).length <=
0
)
DistinctArray.push(e);
} else {
if (DistinctArray.indexOf(e) === -1) DistinctArray.push(e);
}
});
} catch (error) {
DistinctArray = [];
}
return DistinctArray;
}
Demo 2