gpt4 book ai didi

javascript - 带 TS 的 Angular 2 ES5 滤镜

转载 作者:行者123 更新时间:2023-12-01 03:34:19 24 4
gpt4 key购买 nike

我无法使 ES5 过滤器功能与 Angular 2(使用 TypeScript)一起使用。

我的过滤函数如下所示:

getApplicableNotes(): void {
this.noteService
.getNotes()
.then(notes => {
notes.filter((note) => !note._deleted && !note._done);
this.notes = notes;
})
.catch((error) => this.error = error);
}

我的 TypeScript 类非常简单:

export class Note {
id: number;
title: string;
description: string;
_starred = false;
_done = false;
_deleted = false;

constructor(id: number, title: string, description: string, starred?: boolean, done?: boolean, deleted?: boolean) {
this.id = id;
this.title = title;
this.description = description;
this._starred = starred ? starred : false;
this._done = done ? done : false;
this._deleted = deleted ? deleted : false;
};

}

尽管如此,无论我在 Note 构造函数中设置什么属性,我的注释数组都不会被过滤。

最佳答案

filter()方法不会修改数组,而是返回一个新的过滤数组。您应该将结果分配给 this.notes:

.then(notes => {
this.notes = notes.filter((note) => !note._deleted && !note._done);
})

关于javascript - 带 TS 的 Angular 2 ES5 滤镜,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44358202/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com