Karuna Dande
Karuna Dande

Reputation: 93

How to compare two objects in Angular not using reference of object

how typescript comparing two objects?

originaltraits:{
artistic:25,
athletic:24,
goodLooks:70,
happiness:0,
health:81
}
newtraits:{
artistic:25,
athletic:24,
goodLooks:70,
happiness:0,
health:81
}

I have these two objects I want to compare these objects' fields not using object reference in Angular then how to compare?

Why does two equal Objects shows 'not equal" in Angular 2

Upvotes: 6

Views: 19329

Answers (2)

You can compare objects using JSON.stringify(), assuming you have the same order of object properties. i.e:

JSON.stringify(obj1).toLowerCase() === JSON.stringify(obj2).toLowerCase();

Upvotes: 20

José Quinto Zamora
José Quinto Zamora

Reputation: 2118

If you need to do more comparisons like this one on your project, I highly recommend you using library like deep-equal:

https://www.npmjs.com/package/deep-equal

It has their types to be used with TypeScript:

https://www.npmjs.com/package/@types/deep-equal

Upvotes: 2

Related Questions