Home >Web Front-end >JS Tutorial >Detailed explanation of JS examples of comparing whether the values ​​​​of two Json objects are equal_javascript skills

Detailed explanation of JS examples of comparing whether the values ​​​​of two Json objects are equal_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:13:461906browse

Copy code The code is as follows:

//比较数组是否相同
  modeler.compArray=function(array1,array2)
  {
   if((array1&&typeof array1 ==="object"&&array1.constructor===Array)&&(array2&&typeof array2 ==="object"&&array2.constructor===Array))
   {
      if(array1.length==array2.length)
      {
       for(var i=0;i       {
        var ggg=modeler.compObj(array1[i],array2[i]);
        if(!ggg)
        {
         return false;
        }

       }

      }
      else
      {
       return false;
      }
   }
   else
   {
    throw new Error("argunment is  error ;");
   }

return true;
};
modeler.compObj=function(obj1,obj2)//Compare two objects for equality, excluding actual attribute calculations
{
if(( obj1&&typeof obj1==="object")&&((obj2&&typeof obj2==="object")))
{ var count1=modeler.propertyLength(obj1);
var count2=modeler.propertyLength( obj2);
if(count1==count2)
{
for(var ob in obj1)
{
if(obj1.hasOwnProperty(ob)&&obj2.hasOwnProperty(ob))
{ [ob],obj2[ob]))
                                                                                                                                     ==="string"&&typeof obj2[ob ; }
                                                                                                                              ​[ob],obj2[ob]))
                                                                                                                                                    🎜> }
}
else
return false;
}
}
else
{
Return false;
}
}

return true;
};
modeler.propertyLength=function(obj)//Get the number of properties on the object, excluding the properties on the object prototype
{
var count =0;
if(obj&&typeof obj==="object") {
for(var ooo in obj) {
if(obj.hasOwnProperty(ooo)) {
count ;
}
}
return count;
}else {
throw new Error("argunment can not be null;");
}

};



Test data:




Copy code


The code is as follows:


var data01=[{value:[{id:'asa',value:'dfs'},{}]}];
var data02=[{value:[{id:'asa',value:'dfs' },{}]}];
try {
var jjj=modeler.compArray(data01,data02);

}catch(e) {

}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn