jQuery object equality

前端 未结 7 1742
醉梦人生
醉梦人生 2020-11-22 14:37

How do I determine if two jQuery objects are equal? I would like to be able to search an array for a particular jQuery object.

$.inArray(jqobj, my_array);//-         


        
相关标签:
7条回答
  • 2020-11-22 14:46

    If you want to check contents are equal or not then just use JSON.stringify(obj)

    Eg - var a ={key:val};

    var b ={key:val};

    JSON.stringify(a) == JSON.stringify(b) -----> If contents are same you gets true.

    0 讨论(0)
  • 2020-11-22 14:48

    Use Underscore.js isEqual method http://underscorejs.org/#isEqual

    0 讨论(0)
  • 2020-11-22 14:52

    The $.fn.equals(...) solution is probably the cleanest and most elegant one.

    I have tried something quick and dirty like this:

    JSON.stringify(a) == JSON.stringify(b)
    

    It is probably expensive, but the comfortable thing is that it is implicitly recursive, while the elegant solution is not.

    Just my 2 cents.

    0 讨论(0)
  • 2020-11-22 14:53

    If you still don't know, you can get back the original object by:

    alert($("#deviceTypeRoot")[0] == $("#deviceTypeRoot")[0]); //True
    alert($("#deviceTypeRoot")[0] === $("#deviceTypeRoot")[0]);//True
    

    because $("#deviceTypeRoot") also returns an array of objects which the selector has selected.

    0 讨论(0)
  • 2020-11-22 14:54

    It is, generally speaking, a bad idea to compare $(foo) with $(foo) as that is functionally equivalent to the following comparison:

    <html>
    <head>
    <script language='javascript'>
        function foo(bar) {
            return ({ "object": bar });
        }
    
        $ = foo;
    
        if ( $("a") == $("a") ) {
            alert ("JS engine screw-up");
        }
        else {
            alert ("Expected result");
        }
    
    </script>
    
    </head>
    </html>
    

    Of course you would never expect "JS engine screw-up". I use "$" just to make it clear what jQuery is doing.

    Whenever you call $("#foo") you are actually doing a jQuery("#foo") which returns a new object. So comparing them and expecting same object is not correct.

    However what you CAN do may be is something like:

    <html>
    <head>
    <script language='javascript'>
        function foo(bar) {
            return ({ "object": bar });
        }
    
        $ = foo;
    
        if ( $("a").object == $("a").object ) {
            alert ("Yep! Now it works");
        }
        else {
            alert ("This should not happen");
        }
    
    </script>
    
    </head>
    </html>
    

    So really you should perhaps compare the ID elements of the jQuery objects in your real program so something like

    ... 
    $(someIdSelector).attr("id") == $(someOtherIdSelector).attr("id")
    

    is more appropriate.

    0 讨论(0)
  • 2020-11-22 14:54

    First order your object based on key using this function

    function sortObject(o) {
        return Object.keys(o).sort().reduce((r, k) => (r[k] = o[k], r), {});
    }
    

    Then, compare the stringified version of your object, using this funtion

    function isEqualObject(a,b){
        return JSON.stringify(sortObject(a)) == JSON.stringify(sortObject(b));
    }
    

    Here is an example

    Assuming objects keys are ordered differently and are of the same values

    var obj1 = {"hello":"hi","world":"earth"}
    var obj2 = {"world":"earth","hello":"hi"}
    
    isEqualObject(obj1,obj2);//returns true
    
    0 讨论(0)
提交回复
热议问题