2

Possible Duplicate:
Difference in JSON objects using Javascript/JQuery

How do I return properties that have changed (diff) between two objects?

In my code, I have set a global variable products_diy to store data. When I change the values of the filters on the page, values in products_diy will change accordingly. I would want to get the values that changed (diff) between the original and the current values of products_diy.

too hard for me to write this comparing function. any one helps?

function obj_diff(o2, o1) {
    var obj = {};
    for (var i in o1) {
        var item_type = typeof o2[i];
        if (item_type != 'undefined') {
            //...
        }
    }
    return obj;
}

var products_diy1 = {
    'zone': [1, 2],
    'category': {
        'c0': '1'
    },
    'fitler': {
        'price': {
            'switcher': 'true',
            'price_start': '30',
            'price_end': '50'
        }
    }
}
var products_diy2 = {
    'zone': [1, 2, 3],
    'category': {
        'c0': '1'
    },
    'fitler': {
        'price': {
            'switcher': 'true',
            'price_start': '30',
            'price_end': '80'
        }
    }
}
/* ------- 
compare 2 objects
if the item in o2 is not the same with the o1, 
join item to returned obj

obj_diff = {
    'zone': [1, 2, 3],
    'fitler': {
        'price': {
            'switcher': 'true',
            'price_start': '30',
            'price_end': '80'
        }
    }
}



------- */
Community
  • 1
  • 1
linjuming
  • 2,117
  • 4
  • 23
  • 32

0 Answers0