• 比较两个Json对象是否相等


    一个前端同事遇到的面试题,抽空写了写,也算是个积累

    1.先准备三个工具方法,用于判断是否是对象类型,是否是数组,获取对象长度

     1     function isObj(object) {
     2         return object && typeof (object) == 'object' && Object.prototype.toString.call(object).toLowerCase() == "[object object]";
     3     }
     4 
     5     function isArray(object) {
     6         return object && typeof (object) == 'object' && object.constructor == Array;
     7     }
     8 
     9     function getLength(object) {
    10         var count = 0;
    11         for (var i in object) count++;
    12         return count;
    13     }

    2.准备两个相同或不同的Json对象

     1    var jsonObjA = {
     2         "Name": "MyName",
     3         "Company": "MyCompany",
     4         "Infos": [
     5              { "Age": "100" },
     6              {
     7                  "Box": [
     8                     { "Height": "100" },
     9                     { "Weight": "200" }
    10                  ]
    11              }
    12         ],
    13         "Address": "马栏山"
    14     }
    15     var jsonObjB = {
    16         "Name": "MyName",
    17         "Company": "MyCompany",
    18         "Infos": [
    19             { "Age": "100" },
    20             {
    21                 "Box": [
    22                     { "Height": "100" },
    23                     { "Weight": "200" }
    24                 ]
    25             }
    26         ],
    27         "Address": "马栏山二号"
    28     }

    3.主要的代码

     1     function Compare(objA, objB) {
     2         if (!isObj(objA) || !isObj(objB)) return false; //判断类型是否正确
     3         if (getLength(objA) != getLength(objB)) return false; //判断长度是否一致
     4         return CompareObj(objA, objB, true);//默认为true
     5     }
     6 
     7     function CompareObj(objA, objB, flag) {
     8         for (var key in objA) {
     9             if (!flag) //跳出整个循环
    10                 break;
    11             if (!objB.hasOwnProperty(key)) { flag = false; break; }
    12             if (!isArray(objA[key])) { //子级不是数组时,比较属性值
    13                 if (objB[key] != objA[key]) { flag = false; break; }
    14             } else {
    15                 if (!isArray(objB[key])) { flag = false; break; }
    16                 var oA = objA[key], oB = objB[key];
    17                 if (oA.length != oB.length) { flag = false; break; }
    18                 for (var k in oA) {
    19                     if (!flag) //这里跳出循环是为了不让递归继续
    20                         break;
    21                     flag = CompareObj(oA[k], oB[k], flag);
    22                 }
    23             }
    24         }
    25         return flag;
    26     }

    4.调用方法

    1   var result = Compare(jsonObjA, jsonObjB);
    2   console.log(result); // true or false
    作者:Harry

    本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接.
  • 相关阅读:
    Merge Intervals
    Merge k Sorted Lists
    Sqrt(x)
    First Missing Positive
    Construct Binary Tree from Inorder and Postorder Traversal
    Construct Binary Tree from Preorder and Inorder Traversal
    Distinct Subsequences
    Reverse Nodes in k-Group
    Jump Game II
    Jump Game
  • 原文地址:https://www.cnblogs.com/simendancer/p/5666793.html
Copyright © 2020-2023  润新知