An Interest In:
Web News this Week
- March 20, 2024
- March 19, 2024
- March 18, 2024
- March 17, 2024
- March 16, 2024
- March 15, 2024
- March 14, 2024
Object Equality in JavaScript
It's really easy to compare number or strings but did you try comparing two objects
Even if two object has same key and value pair it will return false.
example:
let name = { firstName: "suprabha", lastName: "supi"}let fullName = { firstName: "suprabha", lastName: "supi"}console.log(name === name) // trueconsole.log(name === fullName) // falseconsole.log(name == fullName) // falseconsole.log(Object.is(name, fullName)) // falseconsole.log(Object.is(name, name)) // true
As you can see above example, both name
and fullName
are identical. Yet, the object are not equal either with ==
or ===
.
There are two things you can check while doing object equality:
1 Objects has same instance
2 Objects has same value
1 Objects has same instance
JavaScript has two approaches to match the values.
- For Primitive Type(string, numbers), it compare by their values.
- For Non-Primitive Type(object, array, date), it compare by their reference.
What does it mean by compare by their reference?
Comparing by reference means object refers to the same location in memory.
example:
let name = { firstName: "suprabha", lastName: "supi"}let fullName = { firstName: "suprabha", lastName: "supi"}let copyName = fullNameconsole.log(name === fullName) // falseconsole.log(copyName == fullName) // true
Here, copyName
and fullName
are referring to the same instance of memory and hence returning true.
2 Objects has same value
To check the instance you can use ===
equality, but to match the value you need to work more
let name = { firstName: "suprabha", lastName: "supi"}let fullName = { firstName: "suprabha", lastName: "supi"}function isEqual(obj1, obj2) { var props1 = Object.getOwnPropertyNames(obj1); var props2 = Object.getOwnPropertyNames(obj2); if (props1.length != props2.length) { return false; } for (var i = 0; i < props1.length; i++) { let val1 = obj1[props1[i]]; let val2 = obj2[props1[i]]; let isObjects = isObject(val1) && isObject(val2); if (isObjects && !isEqual(val1, val2) || !isObjects && val1 !== val2) { return false; } } return true;}function isObject(object) { return object != null && typeof object === 'object';}console.log(isEqual(name, fullName)); // true
There are few plugins which helps you in terms of the above condition where you can simply use _.isEqual
to check the object values:
let name = { firstName: "suprabha", lastName: "supi"}let fullName = { firstName: "suprabha", lastName: "supi"}console.log(_.isEqual(name, fullName)); // true
Summary
In object, if you do instance check for two objects then you can use ==
, ===
and Object.is()
.
However if you want to check for two object value, then you have to write your own logic to do it.
Thanks for reading the article
Original Link: https://dev.to/suprabhasupi/object-equality-in-javascript-15ff
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To