js one object equal to another object checker All In One
js one object equal to another object checker All In One
js 判断两个对象是否相等
-
对象是引用类型,比较的是引用地址是否相等
-
Primitive values 是原始类型,比较的是值是否相等
JavaScript types
The set of types in the JavaScript language consists of primitive values
and objects
.
- Primitive values (immutable datum represented directly at the lowest level of the language)
Boolean type
Null type
Undefined type
Number type
BigInt type (ES6+)
String type
Symbol type (ES6)
- Objects (collections of properties)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures
Equality Comparison
JavaScript provides three different value-comparison operations:
===
- Strict Equality Comparison ("strict equality", "identity", "triple equals")
==
- Abstract Equality Comparison ("loose equality", "double equals")
Object.is
provides SameValue (new in ES2015).
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness
Object.is
& ===
const obj1 = {o: {b: {j: {value: 1}}}};
// {o: {…}}o: b: j: {value: 1}[[Prototype]]: Object[[Prototype]]: Object[[Prototype]]: Object
const obj2 = {o: {b: {j: {value: 2}}}};
// {o: {…}}
const obj = obj1;
// {o: {…}}
obj1 === obj1;
// true
obj1 === obj2;
// false
obj1 === obj;
// true
Object.is(obj1, obj2);
// false
Object.is(obj1, obj);
// true
const temp = {o: {b: {j: {value: 1}}}};
obj1 === temp;
// false
Object.is(obj1, temp);
// false
比较对象所有属性是否相等
const obj1 = {o: {b: {j: {value: 1}}}};
// {o: {…}}o: b: j: {value: 1}[[Prototype]]: Object[[Prototype]]: Object[[Prototype]]: Object
const obj2 = {o: {b: {j: {value: 2}}}};
// {o: {…}}
const obj = obj1;
// {o: {…}}
Object.is(JSON.stringify(obj1), JSON.stringify(obj2));
// false
Object.is(JSON.stringify(obj1), JSON.stringify(obj));
// true
// ✅
const temp = {o: {b: {j: {value: 1}}}};
Object.is(JSON.stringify(obj1), JSON.stringify(temp));
// true
- 手动实现,递归判断 👎
没必要, waste of time (life is short I need python)
// life is short I don't need that at all!
// 人生苦短,我根本不需要!
refs
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
©xgqfrms 2012-2025
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/16011952.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
2021-03-16 nslookup & ip address All In One
2021-03-16 NPC All in One
2020-03-16 CSS Dark Mode
2020-03-16 ES2020 All in One
2020-03-16 eui & search select
2020-03-16 canvas graph chart
2020-03-16 Error: Cannot find module 'spdy'