1. #1
    Sencha User
    Join Date
    Sep 2008
    Posts
    232
    Vote Rating
    0
    sz_146 is on a distinguished road

      0  

    Default Compare javascript objects

    Compare javascript objects


    Is there an easy way to compare 2 separate custom objects for all thier properties if they are the same ?

  2. #2
    Sencha - Ext JS Dev Team evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    16,943
    Vote Rating
    636
    evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute

      0  

    Default


    Something like:

    Code:
    var same = true;
    for (var i in o1){
        if (o1[i] !== o2[i]){
            same = false;
            break;
        }
    }
    Evan Trimboli
    Sencha Developer
    Twitter - @evantrimboli
    Don't be afraid of the source code!

  3. #3
    Sencha - Community Support Team Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    91
    Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of

      0  

    Default


    Something like:
    Code:
    function compareObject(o1, o2){
    	for(var p in o1){
    		if(o1[p] !== o2[p]){
    			return false;
    		}
    	}
    	for(var p in o2){
    		if(o1[p] !== o2[p]){
    			return false;
    		}
    	}
    	return true;
    }

  4. #4
    Sencha User
    Join Date
    Sep 2008
    Posts
    232
    Vote Rating
    0
    sz_146 is on a distinguished road

      0  

    Default Compare javascript objects

    Compare javascript objects


    Thanks.

  5. #5
    Ext JS Premium Member
    Join Date
    Mar 2008
    Location
    Phoenix, AZ
    Posts
    627
    Vote Rating
    10
    zombeerose will become famous soon enough zombeerose will become famous soon enough

      0  

    Default


    I expanded the object comparison for recursion.

    Code:
    Ext.namespace('Ext.ux.util');
    
    /**
     * Custom routines for working with objects.
     */
    Ext.ux.util.Object = function() {
        //private
        function isEqual(a,b){
            for(var p in a){
                var av = a[p], bv = b[p];
                //recursion
                if (Ext.isObject(av) || Ext.isObject(bv) ) {
                    if (Ext.ux.util.Object.compare(av,bv) !== true){
                        return false;
                    }
                } else { //simple comparisons
                    if(a[p] !== b[p]){
                        return false;
                    }
                }
            }
            return true;
        }
        
        //public
        return {
            /**
             * Compares two objects to determine if they are identical.
             * @param {Object} o1
             * @param {Object} o2
             * @return Boolean
             */
            compare: function(o1, o2){
                //can't use json encoding in case objects contain functions - recursion will fail
                
                //can't compare non-objects
                if (!(Ext.isObject(o1) && Ext.isObject(o2))) return false;
                
                if (isEqual(o1,o2) !== true) return false;
                if (isEqual(o2,o1) !== true) return false;
                
                return true;
            }
        };
    }();

  6. #6
    Sencha - Community Support Team mschwartz's Avatar
    Join Date
    Nov 2008
    Location
    San Diego, Peoples' Republic of California
    Posts
    2,053
    Vote Rating
    17
    mschwartz will become famous soon enough mschwartz will become famous soon enough

      0  

    Default


    If you don't care about properties that are functions:

    Ext.encode(obj1) === Ext.encode(obj2)

  7. #7
    Sencha User
    Join Date
    Jan 2012
    Posts
    1
    Vote Rating
    0
    rickey121 is on a distinguished road

      0  

    Default


    this would be a great instance to make use of the jQuery library.
    Struts2 Jquery
    In this there are codes of jquery and you will get to know how to make best use of it. Also provided a video for better understanding.

  8. #8
    Sencha User
    Join Date
    Mar 2012
    Posts
    1
    Vote Rating
    1
    tekmaverick is on a distinguished road

      1  

    Default


    Quote Originally Posted by sz_146 View Post
    Is there an easy way to compare 2 separate custom objects for all thier properties if they are the same ?
    I have implemented a tail call optimized object equality checker. It does not handle functions as of now. It does good with key value pairs. You can find it at https://github.com/nmishra/helpers.js

  9. #9
    Ext JS Premium Member
    Join Date
    Oct 2010
    Location
    Freiburg
    Posts
    23
    Vote Rating
    1
    chrisjs is on a distinguished road

      0  

    Default


    Hello,

    thank you for the work of this helper function for comparing objects. It is working very well. I am actually in refactoring an ExtJS 4 MVC app so it uses autoloading. Therefor I want to rewrite this function to a form which is called with
    Code:
    Ext.define()
    as I think, it could not be loaded by the autoloader like this.

  10. #10
    Sencha User
    Join Date
    Apr 2013
    Posts
    2
    Vote Rating
    0
    stamat is on a distinguished road

      0  

    Default Deep object comparison

    Deep object comparison


    I evaded this topic for some time, cause I was bored to think about it. Past two days I devised a simple and extensible solution to check if two objects are equal: http://stamat.wordpress.com/2013/06/22/javascript-object-comparison/

    The code is as follows:
    Code:
    //Returns the object's class, Array, Date, RegExp, Object are of interest to us
    var getClass = function(val) {
    	return Object.prototype.toString.call(val)
    		.match(/^\[object\s(.*)\]$/)[1];
    };
     
    //Defines the type of the value, extended typeof
    var whatis = function(val) {
     
    	if (val === undefined)
    		return 'undefined';
    	if (val === null)
    		return 'null';
     
    	var type = typeof val;
     
    	if (type === 'object')
    		type = getClass(val).toLowerCase();
     
    	if (type === 'number') {
    		if (val.toString().indexOf('.') > 0)
    			return 'float';
    		else
    			return 'integer';
    	}
     
    	return type;
    };
     
    var compareObjects = function(a, b) {
    	if (a === b)
    		return true;
    	for (var i in a) {
    		if (b.hasOwnProperty(i)) {
    			if (!equal(a[i],b[i])) return false;
    		} else {
    			return false;
    		}
    	}
     
    	for (var i in b) {
    		if (!a.hasOwnProperty(i)) {
    			return false;
    		}
    	}
    	return true;
    };
     
    var compareArrays = function(a, b) {
    	if (a === b)
    		return true;
    	if (a.length !== b.length)
    		return false;
    	for (var i = 0; i < a.length; i++){
    		if(!equal(a[i], b[i])) return false;
    	};
    	return true;
    };
     
    var _equal = {},
    _equal.array = compareArrays;
    _equal.object = compareObjects;
    _equal.date = function(a, b) {
    	return a.getTime() === b.getTime();
    };
    _equal.regexp = function(a, b) {
    	return a.toString() === b.toString();
    };
    //	uncoment to support function as string compare
    //	_equal.fucntion =  _equal.regexp;
     
     
     
    /*
     * Are two values equal, deep compare for objects and arrays.
     * @param a {any}
     * @param b {any}
     * @return {boolean} Are equal?
     */
    var equal = function(a, b) {
    	if (a !== b) {
    		var atype = whatis(a), btype = whatis(b);
     
    		if (atype === btype && _equal.hasOwnProperty(atype))
    			return _equal[atype](a, b);
     
    		return false;
    	}
     
    	return true;
    };