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. curl获取远程文件内容

    curl获取远程文件内容 ** 获取远程文件内容 @param $url 文件http地址 * function fopen_url($url) { if (function_exists(& ...

  2. 121. Best Time to Buy and Sell Stock(股票最大收益)

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  3. CCPC 2016-2017, Finals Solution

    A - The Third Cup is Free 水. #include<bits/stdc++.h> using namespace std; ; int n; int arr[max ...

  4. 两个星期,用Flutter撸个APP

    前言 Flutter是Google推出的跨平台的解决方案,Slogan是"Design beautiful apps",国内也有知名企业在使用和推广,例如阿里.美团都有在尝试. 个 ...

  5. 浅谈history对象以及路由插件原理

    简介 History对象最初设计用来表示窗口的浏览历史,但是,出于隐私方面的原因,History对象不再允许脚本访问已经访问过的实际URL.虽然,我们不清楚历史URL,但是,我们可以通过History ...

  6. Python笔记 #08# NumPy: Statistic Basis

    数据分析的基本步骤: 了解你的数据(get to know your data), 做一些统计学处理(像僵尸一样盯着数字不会带给你任何灵感!) 实现可视化(get a better feeling f ...

  7. pyDay9

    内容来自廖雪峰的官方网站. generator 1.引入generator的原因. 通过列表生成式,我们可以直接创建一个列表.但是,受到内存限制,列表容量肯定是有限的.而且,创建一个包含100万个元素 ...

  8. 使用Git【转】

    本文转载自:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 我们一直用GitHub ...

  9. HDU 2848 Number Cutting Game(博弈思想 + dfs)题解

    思路:dfs找先手必胜的情况是否存在 代码: #include<stack> #include<vector> #include<queue> #include&l ...

  10. python 字典输出键值对

    d = {, , } for dict_key, dict_value in d.items(): print(dict_key,'->',dict_value)