比较两个JSON对象 [英] Comparing Two JSON Objects

查看:124
本文介绍了比较两个JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这只是来自object-1的一条数据.整个对象中大约有4000个这样的对象.

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

这是来自object-2的一个值.该对象要小得多,在object-2内最多只能有100个值.

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

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

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

推荐答案

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

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.

如果您需要知道公共道具在两个对象中的位置,则可以更改地图以保存父对象,而不仅仅是将其设置为true,但是您必须处理以下问题:该属性的出现不止一次.我没有进行详细说明,因为您对要执行的操作的说明没有详细说明,因此我在此处留了一个函数,该函数显示了如何递归地遍历对象中的所有属性.

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

正在运行的演示: http://jsfiddle.net/jfriend00/ESBZv/

这篇关于比较两个JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆