function objectEquals(object1: Object, object2: Object): boolean {
  for (let propName in object1) {
    if (object1.hasOwnProperty(propName) != object2.hasOwnProperty(propName)) {
      return false
    } else if (typeof object1[propName] != typeof object2[propName]) {
      return false
    }
  }
  for (let propName in object2) {
    if (object1.hasOwnProperty(propName) != object2.hasOwnProperty(propName)) {
      return false
    } else if (typeof object1[propName] != typeof object2[propName]) {
      return false
    }
    if (!object1.hasOwnProperty(propName)) {
    continue
  }
  if (object1[propName] instanceof Array && object2[propName] instanceof Array) {
    if (!arrayEquals(object1[propName], object2[propName])) {
      return false
    }
   } else if (object1[propName] instanceof Object && object2[propName] instanceof Object) {
    if (!objectEquals(object1[propName], object2[propName])) {
      return false
    }
   } else if (object1[propName] != object2[propName]) {
    return false
    }
  }
  return true
} function arrayEquals(array1, array2): boolean {
  if (!array1 && !array2) {
    return true
  }
  if (!array1 && array2 || array1 && !array2) {
    return false
  }
  if (array1.length != array2.length) {
    return false
  }
  for (let i = 0, l = array1.length; i < l; i++) {
    if (array1[i] instanceof Array && array2[i] instanceof Array) {
      if (!arrayEquals(array1[i], array2[i]))
        return false
    } else if (array1[i] instanceof Object && array2[i] instanceof Object) {
      if (!objectEquals(array1[i], array2[i]))
        return false
    } else if (array1[i] != array2[i]) {
      return false
    }
  }
  return true
}

比较两个array或者object是否深度相等的更多相关文章

  1. 不要将 Array、Object 等类型指定给 prototype

    在 JavaScript 中,注意不要将 Array.Object 等类型指定给 prototype,除非您的应用需要那么做.先观察如下代码: function Foo(){}Foo.prototyp ...

  2. Poco::JSON::Array 中object 设置preserveInsertionOrder 时,stringify出错-->深入解析

    在使用poco version 1.6.0时 Poco::JSON::Array 在object  设置preserveInsertionOrder =true 时 调用 array.stringif ...

  3. Javascript中判断变量是 array还是object(是数组还是对象)

    段文字是从github上截取由本人翻译过来的. 原文地址:https://github.com/nathansmith/javascript-quiz/blob/master/ANSWERS.md 怎 ...

  4. typeof升级版,可以识别出array、object、null、nan、[]、{}

    typeof 经常混淆array.object.null等,升级处理一下. 可以将这个函数放在common.js中使用. function getTypeName(v) { var v_str = J ...

  5. AFNetworking 关于JSON text did not start with array or object and option to allow fragments not set 错误

    AFHTTPSessionManager *manager =[AFHTTPSessionManager manager]; [manager GET:@"http://www.baidu. ...

  6. Javascript中判断变量是数组还是对象(array还是object)

    怎样判断一个JavaScript变量是array还是obiect? 答案: 1.如果你只是用typeof来检查该变量,不论是array还是object,都将返回‘objec'. 此问题的一个可行的答案 ...

  7. array to object

    array to object native js & ES6 https://stackoverflow.com/questions/4215737/convert-array-to-obj ...

  8. 判断 js 的 Array 和 Object

    https://my.oschina.net/ohcoding/blog/470952?p=1 var a = ['hello','world']; console.log(typeof a); // ...

  9. this 、typeof、false、parseInt()、this、arguments、Array和object判断

    typeof typeof (undefined) 不会报错 undefined object Number boolean function String 返回值为字符串类型 false .fals ...

随机推荐

  1. Learning Query and Document Similarities from Click-through Bipartite Graph with Metadata

    读了一篇paper,MSRA的Wei Wu的一篇<Learning Query and Document Similarities from Click-through Bipartite Gr ...

  2. 通过.properties配置文件,在Service层获取值

    问题:从配置文件获取不到值的原因:1.静态变量:2.没通过Spring加载该实例对象. 1. conf.properties配置文件内容: 2. Spring加载配置文件内容,spring-confi ...

  3. FFmpeg 入门(6):音频同步

    本文转自:FFmpeg 入门(6):音频同步 | www.samirchen.com 音频同步 上一节我们做了将视频同步到音频时钟,这一节我们反过来,将音频同步到视频.首先,我们要实现一个视频时钟来跟 ...

  4. 【运维技术】slc pm 启动不了,异常排除问题记录

    问题描述 slc pm 启动的时候报错如下: [root@iZuf61qpjpeuqc5mjo4kn8Z lixiang-scf-web]# slc pm strong-remoting deprec ...

  5. Python虚拟环境的安装

    1.升级python包管理工具pip pip install --upgrade pip 备注:当你想升级一个包的时候“pip install --upgrade”包名 2.python虚拟环境的安装 ...

  6. OpenCV图像的轮廓的匹配

    http://blog.sina.com.cn/s/blog_67a7426a0101cxl0.html 一个跟轮廓相关的最常用到的功能是匹配两个轮廓.如果有两个轮廓,如何比较它们;或者如何比较一个轮 ...

  7. [one day one question] safari缓存太厉害

    问题描述: safari缓存太厉害,这怎么破? 解决方案: window.onpageshow = function(event) { if (event.persisted) { window.lo ...

  8. 20145339顿珠达杰 《网络对抗技术》 逆向与Bof基础

    目的 通过一些方法,使能够运行本不该被运行的代码部分,或得到shell的使用: 将正常运行代码部分某处call后的目标地址,修改为另一部分我们希望执行.却本不应该执行的代码部分首地址(这需要我们有一定 ...

  9. Thread.Start和Delegate.BeginInvoke 以及Control.BeginInvoke

    Thread.Start starts a new OS thread to execute the delegate. When the delegate returns, the thread i ...

  10. SpringBoot与Dubbo整合下篇

    (1)pom.xml引入相关依赖jar包,如下: <dependency> <groupId>com.alibaba</groupId> <artifactI ...