我想從附件物件陣列中洗掉附件,該附件物件陣列位于另一個物件陣列內部的另一個注釋物件陣列內。我一直在努力解決這個問題。
export class CommentSection{
id: string;
comments: CommentObject[];
}
export class CommentObject{
id: string;
created: Date;
text: string;
attachments: CommentAttachment[];
}
export class CommentAttachment{
id: string;
created: Date;
filename: string;
}
還有我的帶有草稿解決方案的 ts 檔案,它不起作用(不洗掉附件):
export class CommentsComponent {
public comment: CommentObject;
commentsArray: CommentSection;
public deleteCommentAttachment(attachment: CommentAttachment): void {
this.commentsArray.comments = this.commentsArray.comments.filter((c) =>
c[this.comment.id].attachments.splice(attachment.id, 1),
);
}
}
uj5u.com熱心網友回復:
你也可以轉換成 rxjs 以可讀的步驟來完成,盡管有些人可能會覺得它有點矯枉過正。這是示例:
https://stackblitz.com/edit/angular-ivy-swnhf1?file=src/app/app.component.ts
uj5u.com熱心網友回復:
您想過濾附件而不是評論嗎?splice 還使用索引而不是自定義 id-s。我已經用地圖替換了過濾器。尚未對其進行測驗,但它應該可以作業:)
this.commentsArray.comments = this.commentsArray.comments.map((c) => {
const comment = c[this.comment.id];
const attachments = comment.attachments;
const index = attachments.findIndex(at => at.id == attachment.id);
attachments.splice(index, 1);
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/407270.html
標籤:
