Javascript 防止在使用可观察角度2时插入重复项

Javascript 防止在使用可观察角度2时插入重复项,javascript,arrays,angular,typescript,angular2-observables,Javascript,Arrays,Angular,Typescript,Angular2 Observables,我有一个问题,每次我提交一篇文章时,我的数组都在以指数级增长。我认为它发生在第二个可观察的阶段,因为用户对象在每次发布后都会被更新,以更新他们上次更新发布的时间戳 我试图检查内部可观察到的柱是否已经在数组中,以防止重复插入到数组中。出于某种原因,这是行不通的 loadPosts(url: string) { switch (url) { case '/timeline/top': this.postsService.subscribeAllPos

我有一个问题,每次我提交一篇文章时,我的数组都在以指数级增长。我认为它发生在第二个可观察的阶段,因为用户对象在每次发布后都会被更新,以更新他们上次更新发布的时间戳

我试图检查内部可观察到的柱是否已经在数组中,以防止重复插入到数组中。出于某种原因,这是行不通的

 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;
   }

}
loadPosts(url:string){
开关(url){
案例“/时间线/顶部”:
this.postsService.subscribeAllPosts(this.selectedArea)
.订阅(帖子=>{
让容器=新数组();
为(让职位的职位){
this.getEquipedItemsForUserName(post.username).subscribe(x=>{
试一试{
if(container.indexOf(新的PostContainer(post,x[0].equipItems))=-1){
container.push(新的PostContainer(post,x[0].equipItems));
}
console.log(container);//在每个提交的帖子之后呈指数增长
}捕获(ex){}
}
);
}
this.postContainers=container;//postContainers是在html中循环的数组。
});
打破
}
}

您的问题是,通过创建一个新的
PostContainer
您正在创建一个不在
容器中的新对象,因此它将在
posts
中添加每个
post

您应该检查
post
的某些唯一值是否不存在于
容器的任何项中

比如:

if (container.findIndex((postContainer) => postContainer.id === post.id) === -1) {
    continer.push(new PostContainer(post, x[0].equippedItems));
}

您的问题是,通过创建一个新的
PostContainer
您正在创建一个不在
容器中的新对象,因此它将在
posts
中添加每个
post

您应该检查
post
的某些唯一值是否不存在于
容器的任何项中

比如:

if (container.findIndex((postContainer) => postContainer.id === post.id) === -1) {
    continer.push(new PostContainer(post, x[0].equippedItems));
}

我不确定您对这个问题的看法是否正确,从您的帖子中删除重复内容将很容易,如下所示:

this.postsService.subscribeAllPosts(this.selectedArea)
            .distinct()
            .subscribe(posts => {
                ...
            });

我不确定您对这个问题的看法是否正确,从您的帖子中删除重复内容将很容易,如下所示:

this.postsService.subscribeAllPosts(this.selectedArea)
            .distinct()
            .subscribe(posts => {
                ...
            });

@弗雷德:这是一个很好的建议,我自己没有提到过,但重要的是要利用可观测的力量,而不是重新发明wheel@Fred这是一个非常好的提示,我自己没有提到过,但重要的是要利用可观测的力量,而不是重新发明轮子