I'm trying to find a faster way to compare two json objects. Currently, we have a function that has about 7 $.each() calls in it, which I believe is a very inefficient way to do this, but I have no idea how to do it faster. I'll post our function that compares the two objects, and also, a sample of the two objects.
This is just one piece of data from object-1. There are like 4000 of these within the entire object.
{
"aaData": [
{
"serial":"LRRFNGHX",
"model":"Dell Optiplex",
"os":"Windows NT",
"man":"Dell",
"group":"558D",
"pcName":"LID93740SHD0",
"department":"HR",
"customerName":"Bill gates",
"username":"bgates",
"deployLocation":"Chicago, IL",
"currentLocation":"127.0.0.1",
"cnStatus":"Usable",
"previousModel":"Gateway",
"id":"256",
"enabled":"false"
}
]
}
This is one value from object-2. This object is considerably smaller, with only a maximum of maybe 100 values within object-2.
{
"team": {},
"department": {
"Automotive": "Automotive"
},
"os": {},
"man": {},
"model": {},
"cnStatus": {
"Usable": "Usable"
}
}
Here is the ugliest function ever. This compares these two objects, and sets the enabled property in the larger object to true for all matches:
function objCompare(){
newTotal = 0;
var typeCount = 0;
var menuArray = [];
console.log(JsonObj);
$.each(menuObject, function (i, type){
var empty = jQuery.isEmptyObject(type);
if(empty === false){
if(typeCount == 0){
$.each(type, function (j, subtype){
menuArray.push(subtype);
$.each(JsonObj, function(key, element){
element.enabled = "false";
$.each(element, function(key, subelement){
if( (subelement != null) && (menuArray.contains(subelement.replace(/\s/g, ''))) ){
element.enabled = "true";
}
});
});
});
}else if(typeCount >= 1){
$.each(type, function (j, subtype){;
menuArray.push(subtype);
});
$.each(JsonObj, function(key, element){
if((element.enabled === "true") && !menuArray.contains(element[i])){
element.enabled = "false";
}
});
}
typeCount++;
}
if(empty === true){
if(typeCount === 0){
$.each(JsonObj, function(key, element){
element.enabled = "false";
});
}
}
});
}