我有一個問題,每次提交帖子時,我的數組越來越多地呈指數增長。我認爲它發生在第二個可觀察的事件中,因爲該用戶對象在每個帖子後被更新以更新他們上次更新帖子時的時間戳。使用Observable Angular時防止插入重複2
我想檢查裏面observable,如果該職位已經在數組中,以防止重複被插入到數組中。由於某種原因,這是行不通的。
loadPosts(url: string) {
switch (url) {
case '/timeline/top':
this.postsService.subscribeAllPosts(this.selectedArea)
.subscribe(posts => {
let container = new Array<PostContainer>();
for (let post of posts) {
this.getEquippedItemsForUsername(post.username).subscribe(x => {
try {
if (container.indexOf(new PostContainer(post, x[0].equippedItems)) === -1) {
container.push(new PostContainer(post, x[0].equippedItems));
}
console.log(container); // grows exponentially after each submitted post
} catch (ex) { }
}
);
}
this.postContainers = container; // postContainers is the array that is being looped over in the html.
});
break;
}
}
@Fred這是一個很好的提示,我沒有提到它自己,而是利用這一點很重要觀察者的力量,而不是重新發明輪子 – gonzofish