我有一个问题,每次我提交帖子时,我的数组都呈指数级增长。我认为它发生在第二个 observable 中,因为在每个帖子之后都会更新用户对象,以更新他们上次更新帖子的时间戳。
我正在尝试检查内部 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;
}
}