RxJs 映射运算符更改 angular 中对象集合的 属性 值
RxJs map operator to change the property value of a collection of objects in angular
在我的 angular 组件中,我具有以下属性:
memberInfoLists$: Observable<MemberInfo[]>;
total$: Observable<number>;
memberPhotoUrl: string = environment.memberPhotoUrl;
memberDefaultPhoto: string = environment.defaultPersonPhoto;
现在,在 ngOnInit 函数中,我已调用该服务来获取可观察到的数据以填充 memberInfoLists$ 属性。但在填充之前,我想更改集合中每个对象的 属性 值之一。这是我的代码:
ngOnInit(): void {
this.memberInfoLists$ = this.service.memberInfoLists$.pipe(map(item => item.forEach(y => {
if (y.MemberPhotoUrl) {
y.MemberPhotoUrl = this.memberPhotoUrl + y.MemberPhotoUrl;
}
else{
y.MemberPhotoUrl = this.memberDefaultPhoto;
}
if (y.VoterPhotoUrl) {
y.VoterPhotoUrl = this.memberPhotoUrl + y.VoterPhotoUrl;
}
else{
y.VoterPhotoUrl = this.memberDefaultPhoto;
}
return item;
})));
this.total$ = this.service.total$;
}
但它抛出以下异常:
src/app/setup-module/pages/member-info-list/member-info.component.ts:69:3
error TS2322: Type 'Observable' is not assignable to type 'Observable<MemberInfo[]>'. Type 'void' is not assignable to type
'MemberInfo[]'.
this.memberInfoLists$ =
this.service.memberInfoLists$.pipe(map(item => item.forEach(y => {
谁能帮我解决这个问题?
return 语句在错误的地方它应该在 forEach
之后
forEach 应该有大括号
this.memberInfoLists$ = this.service.memberInfoLists$.pipe(
map((item) => {
item.forEach((y) => {
if (y.MemberPhotoUrl) {
y.MemberPhotoUrl = this.memberPhotoUrl + y.MemberPhotoUrl;
} else {
y.MemberPhotoUrl = this.memberDefaultPhoto;
}
if (y.VoterPhotoUrl) {
y.VoterPhotoUrl = this.memberPhotoUrl + y.VoterPhotoUrl;
} else {
y.VoterPhotoUrl = this.memberDefaultPhoto;
}
})
return item;
}
)
);
在我的 angular 组件中,我具有以下属性:
memberInfoLists$: Observable<MemberInfo[]>;
total$: Observable<number>;
memberPhotoUrl: string = environment.memberPhotoUrl;
memberDefaultPhoto: string = environment.defaultPersonPhoto;
现在,在 ngOnInit 函数中,我已调用该服务来获取可观察到的数据以填充 memberInfoLists$ 属性。但在填充之前,我想更改集合中每个对象的 属性 值之一。这是我的代码:
ngOnInit(): void {
this.memberInfoLists$ = this.service.memberInfoLists$.pipe(map(item => item.forEach(y => {
if (y.MemberPhotoUrl) {
y.MemberPhotoUrl = this.memberPhotoUrl + y.MemberPhotoUrl;
}
else{
y.MemberPhotoUrl = this.memberDefaultPhoto;
}
if (y.VoterPhotoUrl) {
y.VoterPhotoUrl = this.memberPhotoUrl + y.VoterPhotoUrl;
}
else{
y.VoterPhotoUrl = this.memberDefaultPhoto;
}
return item;
})));
this.total$ = this.service.total$;
}
但它抛出以下异常:
src/app/setup-module/pages/member-info-list/member-info.component.ts:69:3
error TS2322: Type 'Observable' is not assignable to type 'Observable<MemberInfo[]>'. Type 'void' is not assignable to type 'MemberInfo[]'.
this.memberInfoLists$ = this.service.memberInfoLists$.pipe(map(item => item.forEach(y => {
谁能帮我解决这个问题?
return 语句在错误的地方它应该在 forEach
之后forEach 应该有大括号
this.memberInfoLists$ = this.service.memberInfoLists$.pipe(
map((item) => {
item.forEach((y) => {
if (y.MemberPhotoUrl) {
y.MemberPhotoUrl = this.memberPhotoUrl + y.MemberPhotoUrl;
} else {
y.MemberPhotoUrl = this.memberDefaultPhoto;
}
if (y.VoterPhotoUrl) {
y.VoterPhotoUrl = this.memberPhotoUrl + y.VoterPhotoUrl;
} else {
y.VoterPhotoUrl = this.memberDefaultPhoto;
}
})
return item;
}
)
);