盖伊,我在这里做错了什么? 我试图根据另一个对象中属性的值从一个数组中获取一个项目。 我试过使用.find和.filter,但得到了错误的结果。 要么它返回数组中的第一个项目作为结果,要么返回声明对象,沿着该行的某个位置丢失了要从数组中提取的对象。
getContractTypes() {
this.recruitmentauthorisationservice.getContractTypes().subscribe(data => {
this.contractTypesList = data;
console.log(this.contractTypesList); // All object return succesfully
let rTypeId = this.selectedRequest['rapRequestTypeId'];
console.log('rTypeId: ' + rTypeId); // Correct value of type number returned
this.selectedRequestType = this.contractTypesList.filter(x => x.RAPRequestTypeId === this.selectedRequest.RAPRequestTypeId)[0];
console.log(this.selectedRequestType); //Return's wrong object. i.e first item in the array
//Tried a different way, this return's the empty declaration object when you console.log.
// for (var i = 0; i < this.contractTypesList.length; i++) {
// console.log(this.contractTypesList[i]);
// if(this.contractTypesList[i].RAPRequestTypeId === this.selectedRequest['rapRequestTypeId']){
// this.selectedRequestType = this.contractTypesList[i];
// }
// }
});
}
this.getContractTypes(); is being called in ngOnInit(){}
result object is initialiased like this:
selectedRequestType: ContractType = {
RAPRequestTypeId: 0,
RAPRequestTypeTitle: ''
};
and the object model is defines like this:
export interface ContractType{
RAPRequestTypeId?: number;
RAPRequestTypeTitle?: string
}