推送时重复的元素(角度)

时间:2020-07-20 21:23:55

标签: angular push

我有一个创建新Selections的表单,提交后需要将该对象推送到我的数组中,这样您就可以在表中看到结果而无需重新加载。我有一个功能:

ngOnInit(): void {
//this.selections - filtered array
//selections - full

    this.communicationService.createOrUpdateSelection$.subscribe((response) => {
      this.selections.forEach(selection => {
        if (selection.directionId === response.directionId && selection.locationId === response.locationId) {
          this.selections.push(response);
        } else {
          this.selectionService.getAll().subscribe(selections => {
          selections.push(response);
          })
        }
      });
    });
  }

在此功能中,response是我表单中的一个对象。然后,我检查是否有人用选择的Selectionlocation创建了direction,如果是,则将response推送到过滤后的数组(如果否)到整个数组。 因此,当我在表中创建具有相同的Selectionlocation的{​​{1}}时,它会显示重复项,每次提交后重复项的数量都会增加。但是重新加载后,我只有一个uniq direction

非常感谢您的帮助!

如果需要,CommunicationService:

Selection

0 个答案:

没有答案
相关问题