Objects in js are reference type values, and the comparison of two objects is the comparison of the referenced memory addresses. Like the following code:
var obj1={};
var obj2={};
obj1===obj2 //false
Although these two objects appear to be equal, they refer to different objects in the heap memory, so they are not equal.
The implementation of
inArray should be to traverse the array, compare each item of the array with the target value, and return the index value if they are equal, and -1 if they are not equal. Due to the above reasons, the reference values of the two separately declared objects are not the same. are equal, so -1 is returned.
Objects in js are reference type values, and the comparison of two objects is the comparison of the referenced memory addresses. Like the following code:
Although these two objects appear to be equal, they refer to different objects in the heap memory, so they are not equal.
The implementation ofinArray should be to traverse the array, compare each item of the array with the target value, and return the index value if they are equal, and -1 if they are not equal. Due to the above reasons, the reference values of the two separately declared objects are not the same. are equal, so -1 is returned.
It is generally understood that an array is an object, but the object is not necessarily an array. inArray works on arrays
In the source code, isArray: Array.isArray is used. It can also contain objects inside, but the outer layer must be [].