gpt4 book ai didi

javascript - TypeScript 和 ExactEqual

转载 作者:数据小太阳 更新时间:2023-10-29 05:04:23 25 4
gpt4 key购买 nike

如何最简单的判断两个对象是否相等?我注意到没有类似 ExactEqual() 的方法,所以我想知道我是否应该手动执行此操作。

感谢您的帮助!

最佳答案

没有这样的方法。看看这个jqfaq.com讨论它的链接并给出了最佳检查方法。这将适用于 typescript 和 javascript。希望对您有所帮助!

引用该答案中最重要的部分:

//“===” means that they are identical.
//“==” means that they are equal in value.
//( == )
//Each JavaScript value is of a specific “type” (Numbers, strings, Booleans, functions, and objects). So if you try to compare a string with a number, the browser will try to convert the string into a number before doing the comparison.
//So the following will return true.
55 == “55″ //true
0 == false //true
1 == true //true
//(===)
//The === operator will not do the conversion, if two values are not the same type it will return false. In other words, this returns true only if the operands are strictly equal in value or if they are identical objects.
55 === “55″ //false
0 === false //false
1 === true //false
var a = [1, 2, 3];
var b = [1, 2, 3];
var c = a;
var is_ab_eql = (a === b); // false (Here a and b are the same type,and also have the same value)
var is_ac_eql = (a === c); // true.
//Value types (numbers):
//a === b returns true if a and b have the same value and are of the same type.
//Reference types:
//a === b returns true if a and b reference the exact same object.
//Strings:
//a === b returns true if a and b are both strings and contain the exact same characters.
var a = “ab” + “c”;
var b = “abc”;
a === b //true
a == b //true
//in thiss case the above condition will fail
var a = new String(“abc”);
var b = “abc”;
a === b //false
a == b// true
//… since a and b are not a same type.
typeof “abc”; // ‘string’
typeof new String(“abc”)); // ‘object

关于javascript - TypeScript 和 ExactEqual,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13877649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com