JS compares the values of two Json objects for Equality

Source: Internet
Author: User
Tags hasownproperty

// Compare whether the array is the same
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 <array1.length; 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 whether two objects are equal and do not contain original attributes.
{
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 ))
{

If (obj1 [ob]. constructor = Array & obj2 [ob]. constructor = Array) // if the attribute is an Array
{
If (! Modeler. compArray (obj1 [ob], obj2 [ob])
{
Return false;
};
}
Else if (typeof obj1 [ob] === "string" & typeof obj2 [ob] === "string") // pure attribute
{
If (obj1 [ob]! = Obj2 [ob])
{
Return false;
}
}
Else if (typeof obj1 [ob] === "object" & typeof obj2 [ob] === "object") // The attribute is an object
{
If (! Modeler. compObj (obj1 [ob], obj2 [ob])
{
Return false;
};
}
Else
{
Return false;
}
}
Else
{
Return false;
}
}
}
Else
{
Return false;
}
}

Return true;
};
Modeler. propertyLength = function (obj) // obtain the number of attributes on the object, excluding attributes on the original object.
{
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:

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

} Catch (e ){

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.