简体   繁体   English

如何使用反应形式使用angular8比较对象数组

[英]How to compare array of objects using angular8 using reactive forms

i have used reactive forms, and the values are getting assigned to the html and the reactive forms is due to the array of object.我使用了反应形式,并且值被分配给了 html,反应形式是由于对象数组。 So, what ever action i perform there must be used for comparing objects with the existing object.因此,我在那里执行的任何操作都必须用于将对象与现有对象进行比较。 So i have used one array of object comparison method.所以我使用了一组对象比较方法。 But here the previous value is also getting binded to the new value which has been assigned to the form.但是这里以前的值也被绑定到分配给表单的新值。

I want the newly edited value and the old value as seperate so that i can compare if the object proerty values are diffrent then i can enable for save.我希望新编辑的值和旧值分开,以便我可以比较对象属性值是否不同,然后我可以启用保存。

DEMO: DEMO演示: 演示

TS: TS:

saveDetails() {
 this.objectsAreSame(this.agentDetailsList, this.detailsToggle)
      console.log(this.agentDetailsList);
      console.log(this.detailsToggle);
      console.log(this.objectsAreSame,"this.objectsAreSame")
    }

      objectsAreSame(a1, a2) {
    for (var i = 0, len = a1.length; i < len; i++) {
      for (var j = 0, len = a2.length; j < len-1; j++) {
          if (a1[i].boolValue == a2[j].boolValue) {
              return false
          } else {
            return true
          }
      }
  }
}

FORM:形式:

  private settingsInfoForm() {
    if (!this.agentDetailsList) {
      // Add
      this.agentSettingsInfoForm = this.FB.group({

        agentToogles: this.FB.array([this.detailsToggle]),
      });
      // this.authService.setDetailsData(this.agentSettingsInfoForm);
    } else {
      // Edit
      if (this.agentDetailsList) {
       this.detailsToggle = this.agentDetailsList
       this.agentSettingsInfoForm = this.FB.group({
          agentToogles: this.FB.array([this.detailsToggle]),
      })
      }
        let settingsInfo = this.agentSettingsInfoForm.valueChanges.subscribe(data => {
          this.formEdit = true;
          console.log('agentSettingsInfoForm', this.formEdit)
        })
}

You can use the Array.prototype.reduce function to compare the objects.您可以使用Array.prototype.reduce函数来比较对象。 It cycles through all the elements of the first array and starts with an initial value of true .它循环遍历第一个数组的所有元素,并从初始值true If the item with the current index exist in the second array then it returns the overlap of all previous comparisons and the current elements at index .如果具有当前索引的项目存在于第二个数组中,则它返回所有先前比较与index处的当前元素的重叠。 If it does not exist, then it returns false.如果它不存在,则返回 false。 Once one value is false the result is false .一旦一个值为false ,结果为false

compare(
  array1.map(item => item.boolValue),
  array2.map(item => item.boolValue)
);

function compare(arr1: Array<boolean>, arr2: Array<boolean>) {
  return arr1.reduce((acc, cur, index) => acc && cur === arr2[index], true);
}

I tried to understand your code and that was a little hard time for me since im also quite new to Angular.我试图理解你的代码,这对我来说有点困难,因为我对 Angular 也很陌生。

Reactive forms in angular are a concept to work on as best with observables. Angular 中的反应形式是一个与 observables 一起工作的最佳概念。 RxJs seems to be essential when it comes to check changes in FormGroups: https://rxjs-dev.firebaseapp.com/api在检查 FormGroups 中的更改时,RxJs 似乎是必不可少的: https ://rxjs-dev.firebaseapp.com/api

Angular doc to observables: https://angular.io/guide/observables可观察的角度文档: https : //angular.io/guide/observables

I know its not that easy to understand the docs.我知道理解文档并不容易。

In the code im tryting to show you how to initiate FormGroups by the Formbuilder and listening to changes in your form.在代码中,我试图向您展示如何通过 Formbuilder 启动 FormGroups 并监听表单中的更改。 At the subcription section you see my comparsion with your object and loggig to console.在订阅部分,您会看到我与您的对象的比较并将日志记录到控制台。

I hoped I could helped you.我希望我能帮助你。 Feel free to ask in the comments.欢迎在评论中提问。 StackDemo: https://stackblitz.com/edit/angular-58vovb StackDemo: https ://stackblitz.com/edit/angular-58vovb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM