![]() Compare objects using _.isEqual from Lodash There are a couple of ways you can do this but I will share two of them. So how do we correctly compare the object data? We've seen that in our attempt to compare two object values, we are actually comparing the reference and not the object data. Therefore, array and array2 now have the same value-the same reference. The only way array and array2 can be equal, is if you have something like: let array = īy assigning array to array2, you are assigning the reference that array holds in the stack, to array2: When you compare both arrays like array = array2, you're not exactly comparing = but you're actually comparing refForArray = refForArray2 (ref is short for reference).Īs we saw in the heap illustration, array and array2 have different memory locations, which means they have different references, which then means the variable array is not equal to the variable array2. It is stored on the heap, in a different memory location and the reference is stored on the stack. The same thing for array2 is not stored on the stack. It is stored on the heap, and the memory location of that data is stored on the stack as a reference. Here's what it would look like on the stack and heap:Īs you can see here, for array, is not stored on the stack. If you have two arrays like this: let array = In the case of objects, you are comparing the references (the addresses) and not the exact values. What happens here is that JavaScript checks for the name and name2 variables in the stack, and then it sees that they have equal values, so it's true-they are equal. Here, we compare name and name2 if they are equal. When you compare primitive values, you are comparing static values, which have a fixed size on the stack: let name = "Dillion" Here's an article where I explained the difference in more detail: Primitive and Reference Values Simplified Comparing Primitive and Reference Values The reference value is an address that points to the location of the data in the memory. ![]() These values are dynamic (can contain multiple values, properties, and can be modified over time) and are stored on the heap, with a reference value in the stack, for example: let array = Reference values are of the object types which includes objects, arrays and functions. ![]() These values are fixed and stored on the stack, for example: let name = "Dillion" Primitive values are of the types string, number, boolean, null, undefined, symbol, and BigInt. Think of a primitive value as one value (static, fixed) and a reference value as a group of multiple values or (dynamic) value. To understand why this is the case, you have to understand what primitive and reference values are in JavaScript. The same thing applies to arrays: let arr1 = But comparing them- obj1 = obj2-returns false. They both have the property of name with a value of "Dillion". Why is that the case? □ Let's understand why.Īs you can see here, obj1 and obj2 look similar. In JavaScript, two objects may not be equal even though they appear to be similar.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |