【发布时间】:2020-11-02 01:21:20
【问题描述】:
我在 Angular 7 上工作,我遇到问题:我无法根据 LocId 返回不同或唯一的对象。
我需要从所有位置的对象数组中返回唯一的对象。
allLocations:any[]=[];
ngOnInit()
{
this.locationsService.allLocations.forEach(loc => {
let d = this.locationsService.allLocations.findIndex(x => x.Locid == loc.Locid);
if (d !== -1) {
this.locationsService.allLocations.splice(d, 1);
}
html组件代码如下:
<tr *ngFor="let l of locationsService.allLocations">
<td class="z2-boxstyle1-td-colcontent">
<div> {{l.Locid}} </div>
</td>
</tr>
数组的结果是:
[
{"Locid":40903,"GPS1":"42.5913974,-71.3277873","CompanyID":1000339},
{"Locid":40900,"GPS1":"42.588432,-71.31720900000001","CompanyID":1000339}
{"Locid":40900,"GPS1":"42.588432,-71.31720900000001","CompanyID":1000339}
]
因为两个对象上的 Locid 相同,所以我返回不同的,它必须如下:
预期结果:
[
{"Locid":40903,"GPS1":"42.5913974,-71.3277873","CompanyID":1000339},
{"Locid":40900,"GPS1":"42.588432,-71.31720900000001","CompanyID":1000339}
]
【问题讨论】:
-
感谢您的回复,但我在 html 上使用了这样的位置服务.allLocations,所以我在 html 组件上进行更改以接受您的代码
-
如果 imake 它为 this.locationsService.allLocations = this.locationsService.allLocations.reduce .... 是否可行。
-
我需要按照上面的评论做,因为如果我使 var res=this.locationsService html 将无法工作
标签: javascript typescript angular7 angular-directive angular-components