How To Compare Array Of Objects Using Angular8 Using Reactive Forms
Solution 1:
You can use the Array.prototype.reduce
function to compare the objects. It cycles through all the elements of the first array and starts with an initial value of 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
. If it does not exist, then it returns false. Once one value is false
the result is 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);
}
Solution 2:
I tried to understand your code and that was a little hard time for me since im also quite new to Angular.
Reactive forms in angular are a concept to work on as best with observables. RxJs seems to be essential when it comes to check changes in FormGroups: https://rxjs-dev.firebaseapp.com/api
Angular doc to 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. 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
Post a Comment for "How To Compare Array Of Objects Using Angular8 Using Reactive Forms"