比较两个JSON对象

时间:2014-02-05 17:02:40

标签: javascript arrays json multidimensional-array compare

我正在尝试找到一种比较两个json对象的更快方法。目前,我们有一个函数,它有大约7 $ .each()调用,我认为这是一种非常低效的方法,但我不知道如何更快地完成它。我将发布比较两个对象的函数,以及两个对象的样本。

这只是来自object-1的一段数据。整个对象中有4000个。

{
"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"
  }
 ]
}

这是object-2中的一个值。此对象相当小,在object-2中最多只有100个值。

{
    "team": {},
    "department": {
        "Automotive": "Automotive"
        },
    "os": {},
    "man": {},
    "model": {},
    "cnStatus": {
        "Usable": "Usable"
        }
}

这是有史以来最丑陋的功能。这将比较这两个对象,并将较大对象中的enabled属性设置为true,以用于所有匹配:

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 个答案:

答案 0 :(得分:0)

这是一个递归收集两个对象中的所有属性的函数,然后返回它们之间的任何公共属性名称的数组。如果返回的数组中有.length === 0,则没有公共属性。否则,它包含公共属性名称。如果您需要进行任何进一步检查,您显然可以为这个相当通用的功能添加更多细节。

如果您需要知道两个对象中公共道具的位置,您可以更改地图以保存父对象,而不是仅将其设置为true,但是您必须处理问题,该属性出现多次。我没有达到那个级别的细节,因为你想要做的事情的规范并不详细,所以我在这里留下了一个函数,它显示了如何递归遍历对象中的所有属性。

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;
}

工作演示:http://jsfiddle.net/jfriend00/ESBZv/

相关问题