【发布时间】:2021-05-24 10:33:51
【问题描述】:
您好,我想知道是否有人可以帮助我过滤一组对象。 当我执行以下操作时,它会返回一个数组数组而不是我需要进行特定显示的对象数组
public ngOnInit(): void {
this.checkboxItems.map(x =>
x.formControl.valueChanges.subscribe((state: boolean) => {
const filterOption = Object.keys(x.formControl.parent.controls).find(key => x.formControl.parent.controls[key] === x.formControl);
console.log('test', state, filterOption);
if (state && filterOption) {
this.activatedRoute.data?.pipe(takeUntil(this.destroy$)).subscribe((data: Data) => {
this.timelineModificationsPerYear = data.verticalTimeline?.verticalTimelineModificationPerYears;
this.timelineModificationsPerYear = this.timelineModificationsPerYear.map(verticalTimelineModificationPerYear =>
verticalTimelineModificationPerYear.verticalTimelineModifications?.filter(
verticalTimelineType => verticalTimelineType.type.toLowerCase() === filterOption
)
);
console.log('filter', this.timelineModificationsPerYear);
});
}
})
);
}
【问题讨论】:
-
你是
mapping而是过滤。这里你要看看this.timelineModificationsPerYear.map -
@gorak 我试过 this.timelineModificationsPerYear = this.timelineModificationsPerYear.filter(verticalTimelineModificationPerYear => verticalTimelineModificationPerYear.verticalTimelineModifications?.filter(verticalTimelineType => verticalTimelineType.type.toLowerCase() === filterOption) 但是这确实返回了对象数组,但如果类型与filterOptions匹配,则不会根据条件进行真正的过滤。它返回原始对象。你能帮忙吗?谢谢
标签: javascript arrays angular typescript filter