typescript 如何在不使用对象引用的情况下比较 Angular 中的两个对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43887065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:32:20  来源:igfitidea点击:

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

angulartypescript

提问by Karuna Dande

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?

我有这两个对象我想比较这些对象的字段而不使用 Angular 中的对象引用,那么如何比较?

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

为什么两个相等的对象在 Angular 2 中显示“不相等”

采纳答案by José Quinto Zamora

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

如果你需要在你的项目中做更多这样的比较,我强烈建议你使用像 deep-equal 这样的库:

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

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

It has their types to be used with TypeScript:

它具有可与 TypeScript 一起使用的类型:

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

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

回答by Ricardo D. Ferrancullo III

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

您可以使用 JSON.stringify() 比较对象,假设您具有相同顺序的对象属性。IE:

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