I have a question because I don't understand the indexOf of the arrangement during JS study.
const o = { name: "Jerry" };
const arr = [1, 5, "a", o, true, 5, [1, 2], "9"];
arr.indexOf({ name: "Jerry" }); // -1
arr.indexOf(o); // 3
arr.indexOf([1, 2]); // -1
What I'm curious about is
javascript array
The objects you are comparing are objects, but indexOf() strictly compares object instances.Objects contained in consto and objects contained in arr.indexOf()
look the same, but they are separate objects because the instances of the object are different. So you can't say the two are the same. The same applies to [1, 2]
and arr.indexOf([1, 2])
in constarr
.
// Turn it around in the console.
const A = {name: "A"}; /* 1 instance */ at this point
A === {name: "A"}; /* 1 more new instance in the process of trying to compare, 2 are different, and the result is false */
[1, 2] === [1, 2]; /* This is the same logic, false */
Answer
I learned a lot while writing the answer. I hope it helps.
© 2024 OneMinuteCode. All rights reserved.