问题
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";
});
}
}
});
}
回答1:
Here's a function that recursively collects all properties in two objects and then returns you an array of any common property names between them. If .length === 0
in the returned array, then there are no common properties. Otherwise, it contains the common property names. If you need to do any further checking, you can obviously add more specifics to this fairly generic function.
If you need to know where the common prop is in the two object, you could change the map to save the parent object instead of just setting it to true
, but then you'd have to deal with the issue where there were more than one occurrence of that property. I didn't go to that level of detail because your specification for what you want to do isn't detailed so I left it here with a function that shows how to recursively walk through all properties in an object.
function findCommonProps(obj1, obj2) {
var map1 = {}, map2 = {};
var commonProps = [];
function isArray(item) {
return Object.prototype.toString.call(item) === "[object Array]";
}
function getProps(item, map) {
if (typeof item === "object") {
if (isArray(item)) {
// iterate through all array elements
for (var i = 0; i < item.length; i++) {
getProps(item[i], map);
}
} else {
for (var prop in item) {
map[prop] = true;
// recursively get any nested props
// if this turns out to be an object or array
getProps(item[prop], map);
}
}
}
}
// get all properties in obj1 into a map
getProps(obj1, map1);
getProps(obj2, map2);
for (var prop in map1) {
if (prop in map2) {
commonProps.push(prop);
}
}
return commonProps;
}
Working demo: http://jsfiddle.net/jfriend00/ESBZv/
来源:https://stackoverflow.com/questions/21583665/comparing-two-json-objects