方法一:JSON.stringify()

此方法简单,适用于当两个对象的属性顺序相同的时候。
var user1 = {name : "nerd", org: "dev"};
var user2 = {name : "nerd", org: "dev"};
var eq = user1 == user2;
alert(eq); // gives false var eq1 = JSON.stringify(user1) == JSON.stringify(user2); alert(eq1); // gives true

方法二:深度比较两个对象

深度比较两个对象,当对象的属性类型相同且属性的值相同(对象的顺序可以不一样),两个对象就相等。
function deepCompare(x, y) {
var i, l, leftChain, rightChain; function compare2Objects(x, y) {
var p; // remember that NaN === NaN returns false
// and isNaN(undefined) returns true
if (isNaN(x) && isNaN(y) && typeof x === 'number' && typeof y === 'number') {
return true;
} // Compare primitives and functions.
// Check if both arguments link to the same object.
// Especially useful on the step where we compare prototypes
if (x === y) {
return true;
} // Works in case when functions are created in constructor.
// Comparing dates is a common scenario. Another built-ins?
// We can even handle functions passed across iframes
if ((typeof x === 'function' && typeof y === 'function') ||
(x instanceof Date && y instanceof Date) ||
(x instanceof RegExp && y instanceof RegExp) ||
(x instanceof String && y instanceof String) ||
(x instanceof Number && y instanceof Number)) {
return x.toString() === y.toString();
} // At last checking prototypes as good as we can
if (!(x instanceof Object && y instanceof Object)) {
return false;
} if (x.isPrototypeOf(y) || y.isPrototypeOf(x)) {
return false;
} if (x.constructor !== y.constructor) {
return false;
} if (x.prototype !== y.prototype) {
return false;
} // Check for infinitive linking loops
if (leftChain.indexOf(x) > -1 || rightChain.indexOf(y) > -1) {
return false;
} // Quick checking of one object being a subset of another.
// todo: cache the structure of arguments[0] for performance
for (p in y) {
if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) {
return false;
} else if (typeof y[p] !== typeof x[p]) {
return false;
}
} for (p in x) {
if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) {
return false;
} else if (typeof y[p] !== typeof x[p]) {
return false;
} switch (typeof(x[p])) {
case 'object':
case 'function': leftChain.push(x);
rightChain.push(y); if (!compare2Objects(x[p], y[p])) {
return false;
} leftChain.pop();
rightChain.pop();
break; default:
if (x[p] !== y[p]) {
return false;
}
break;
}
} return true;
} if (arguments.length < 1) {
return true; //Die silently? Don't know how to handle such case, please help...
// throw "Need two or more arguments to compare";
} for (i = 1, l = arguments.length; i < l; i++) { leftChain = []; //Todo: this can be cached
rightChain = []; if (!compare2Objects(arguments[0], arguments[i])) {
return false;
}
} return true;
}

参考资料:

1.http://stackoverflow.com/questions/1068834/object-comparison-in-javascript
2.https://segmentfault.com/a/1190000008187911

《javascript算法--对象的比较》的更多相关文章

  1. 简单物联网:外网访问内网路由器下树莓派Flask服务器

    最近做一个小东西,大概过程就是想在教室,宿舍控制实验室的一些设备. 已经在树莓上搭了一个轻量的flask服务器,在实验室的路由器下,任何设备都是可以访问的:但是有一些限制条件,比如我想在宿舍控制我种花 ...

  2. 利用ssh反向代理以及autossh实现从外网连接内网服务器

    前言 最近遇到这样一个问题,我在实验室架设了一台服务器,给师弟或者小伙伴练习Linux用,然后平时在实验室这边直接连接是没有问题的,都是内网嘛.但是回到宿舍问题出来了,使用校园网的童鞋还是能连接上,使 ...

  3. 外网访问内网Docker容器

    外网访问内网Docker容器 本地安装了Docker容器,只能在局域网内访问,怎样从外网也能访问本地Docker容器? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Docker容器 ...

  4. 外网访问内网SpringBoot

    外网访问内网SpringBoot 本地安装了SpringBoot,只能在局域网内访问,怎样从外网也能访问本地SpringBoot? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装Java 1 ...

  5. 外网访问内网Elasticsearch WEB

    外网访问内网Elasticsearch WEB 本地安装了Elasticsearch,只能在局域网内访问其WEB,怎样从外网也能访问本地Elasticsearch? 本文将介绍具体的实现步骤. 1. ...

  6. 怎样从外网访问内网Rails

    外网访问内网Rails 本地安装了Rails,只能在局域网内访问,怎样从外网也能访问本地Rails? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Rails 默认安装的Rails端口 ...

  7. 怎样从外网访问内网Memcached数据库

    外网访问内网Memcached数据库 本地安装了Memcached数据库,只能在局域网内访问,怎样从外网也能访问本地Memcached数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装 ...

  8. 怎样从外网访问内网CouchDB数据库

    外网访问内网CouchDB数据库 本地安装了CouchDB数据库,只能在局域网内访问,怎样从外网也能访问本地CouchDB数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Cou ...

  9. 怎样从外网访问内网DB2数据库

    外网访问内网DB2数据库 本地安装了DB2数据库,只能在局域网内访问,怎样从外网也能访问本地DB2数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动DB2数据库 默认安装的DB2 ...

  10. 怎样从外网访问内网OpenLDAP数据库

    外网访问内网OpenLDAP数据库 本地安装了OpenLDAP数据库,只能在局域网内访问,怎样从外网也能访问本地OpenLDAP数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动 ...

随机推荐

  1. python cookie

    http://www.jayconrod.com/posts/17/how-to-use-http-cookies-in-python

  2. 顶点纹理shader

    Shader "Custom/VertDisplace" { Properties { _MainTex ("Base (RGB)", 2D) = " ...

  3. N76E003之IAP

    修改FLASH数据通常需要很长时间,不像RAM那样可以实时操作.而且擦除.编程或读取FLASH数据需要遵循相当复杂的时序步骤.N76E003提供方便FALSH编程方式,可以帮助用户通过IAP方式,重新 ...

  4. 用shell查找某目录下的最大文件

    这是一个很有趣的问题,因为作为一个shell菜鸟,我第一时间是没有任何想法的.心里纳闷为什么这样的操作Linux居然没有直接的命令实现这样的查询. 很自然地,第一感觉就是用awk去实现,因为菜鸟我看a ...

  5. 关于GDI+的一些使用基础设置

    一.新建一个MFC的单文档工程,例如工程名字叫GDIPLUSTEST1. 二.在工程的stdafx.h头文件中添加入 #include "gdiplus.h" using name ...

  6. Android学习之PopupWindow

    Android的对话框有两种:PopupWindow和AlertDialog. 详细说明如下: AlertDialog是非阻塞式对话框:AlertDialog弹出时,后台还可以做事情: AlertDi ...

  7. <转>特征工程(二)

    出处: http://blog.csdn.net/longxinchen_ml/article/details/50493845, http://blog.csdn.net/han_xiaoyang/ ...

  8. java框架---->lucene的使用(一)

    Lucene是一个全文检索的框架,apache组织提供了一个用Java实现的全文搜索引擎的开源项目.这里我们对apache的lucene的框架做一个简单的介绍.心甘情愿这四个字,透着一股卑微,但也有藏 ...

  9. 一、K3 Cloud 开发插件《K3 Cloud开发手册》

    1.K3 Cloud打开 提示错误:未能加载文件或程序集“XXXXXXXXX”或它的某一个依赖项 原因:未正常关机导致的Temporary ASP.NET Files文件夹下的文件损坏 解决:删除C: ...

  10. win7系统自带分区工具,能分出逻辑分区

    先把硬盘里除了你装系统的主分区以外的分区全删除运行CMD输入 DISKPART然后输入list disk,找到你要分的盘,假如是要分第1个硬盘的就输入:select disk 0 这样就选择了第一个硬 ...