'How to compare values of two objects in Angular [duplicate]

I am pushing row dynamically on add new button click in my form. The new data will be added in oldData array in the form of object.

I want to match all values of newData object with oldData array, if newData object with same values is available in oldData array of objects, Then code will console error else it push the row.

Can anyone please help me to make my function working.



Solution 1:[1]

I do it like this:

private allElementExists(newData: any[], oldData: any[]): boolean {
    for (const item of newData) {
        var verifyItem = oldData.find(x => x.id === item.id);
        if (verifyItem === undefined) {
            return false;
        }
    }
    return true;
}

Here you have a function that compares the two objets as you putted in your question

Here is an example in stackblitz using this function

https://stackblitz.com/edit/angular-xscnkf?file=src%2Fapp%2Fapp.component.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1