Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Please tell me how to compare two JSObjects.

    I want a sample program which tells me comparision between two JSObjects. Any help would be really appreciated.


    Thanks,
    Suraj G
    Last edited by sur_7887; 01-16-2012 at 11:00 AM.

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    If you just want to check if both names point to the same object then:

    Code:
    if (obj1 === obj2)
    If you want to check if the two objects are complety identical with the same properties and methods attached in the same order and containing the same values but not necessarily being the same object then

    Code:
    if (JSON.stringify(obj1) === JSON.stringify(obj2))
    If the order in which the properties and methods are attached can be different and you just want to confirm that matching properties and methods containing the same values exist then you'd need to loop through all the properties and methods of both objects and check that a corresponding entry exists for the other. Just looping through all the properties and methods of one wouldn't check that there are no extra properties and methods attached to the other.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    sur_7887 (01-17-2012)

  • #3
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Code:
    import java.io.*;
    import java.lang.ref.*;
    import java.lang.reflect.*;
    import java.rmi.server.ObjID;
    import java.security.*;
    import java.util.*;
    
    // Imports for getJSProtectionDomain
    import java.net.InetAddress;
    import java.net.MalformedURLException;
    import java.net.SocketPermission;
    import java.net.UnknownHostException;
    import java.net.URL;
    import java.nio.ByteBuffer;
    import java.text.NumberFormat;
    import java.text.ParseException;
    import sun.plugin2.applet.*;
    import sun.plugin2.liveconnect.*;
    import sun.plugin2.main.client.LiveConnectSupport;
    import sun.plugin2.main.server.AppletID;
    import sun.plugin2.main.server.JVMManager;
    import sun.plugin2.main.server.Plugin;
    import sun.plugin2.main.server.ResultID;
    import sun.plugin2.message.*;
    import sun.plugin2.util.SystemUtil;
    import netscape.javascript.*;
    
    import java.applet.*;
    import java.awt.*;
    import java.io.*;
    import java.util.*;
    import netscape.javascript.*;
    import sun.plugin.liveconnect.*;
    import sun.plugin2.message.*;
    import sun.plugin2.util.SystemUtil;
    
    public class df extends Applet{
            public static JSObject object;
            private static int appletID;
     
            public df(JSObject object,int appletID) {
                this.object = object;
                this.appletID = appletID;
            }
            
    
            public JSObject getObject()   { return object;   }
            public int               getAppletID() {return appletID; }
    
           
           
            public boolean equals(Object arg) {
                if (arg == null || arg.getClass() != getClass())
                    return false;
                df other = (df) arg;
                return (object.equals(other.object) && appletID == other.appletID);
            }
           
            public static void main(String[] args) {
            	try{
    	       
         
              
               JSObject g1 = new JSObject() {
    			
    			@Override
    			public void setSlot(int arg0, Object arg1) throws JSException {
    				// TODO Auto-generated method stub
    				
    			}
    			
    			@Override
    			public void setMember(String arg0, Object arg1) throws JSException {
    				// TODO Auto-generated method stub
    				
    			}
    			
    			@Override
    			public void removeMember(String arg0) throws JSException {
    				// TODO Auto-generated method stub
    				
    			}
    			
    			@Override
    			public Object getSlot(int arg0) throws JSException {
    				// TODO Auto-generated method stub
    				return null;
    			}
    			
    			@Override
    			public Object getMember(String arg0) throws JSException {
    				// TODO Auto-generated method stub
    				return null;
    			}
    			
    			@Override
    			public Object eval(String arg0) throws JSException {
    				// TODO Auto-generated method stub
    				return null;
    			}
    			
    			@Override
    			public Object call(String arg0, Object[] arg1) throws JSException {
    				// TODO Auto-generated method stub
    				return null;
    			}
    		};;;
             JSObject g2=new JSObject() {
    			
    			@Override
    			public void setSlot(int arg0, Object arg1) throws JSException {
    				// TODO Auto-generated method stub
    				
    			}
    			
    			@Override
    			public void setMember(String arg0, Object arg1) throws JSException {
    				// TODO Auto-generated method stub
    				
    			}
    			
    			@Override
    			public void removeMember(String arg0) throws JSException {
    				// TODO Auto-generated method stub
    				
    			}
    			
    			@Override
    			public Object getSlot(int arg0) throws JSException {
    				// TODO Auto-generated method stub
    				return null;
    			}
    			
    			@Override
    			public Object getMember(String arg0) throws JSException {
    				// TODO Auto-generated method stub
    				return null;
    			}
    			
    			@Override
    			public Object eval(String arg0) throws JSException {
    				// TODO Auto-generated method stub
    				return null;
    			}
    			
    			@Override
    			public Object call(String arg0, Object[] arg1) throws JSException {
    				// TODO Auto-generated method stub
    				return null;
    			}
    		};;;
             System.out.println(g1.equals(g2));
           	}catch(Exception e){e.printStackTrace();}
               
            }
            
            
        }
    In above code its returning me false na.. similarly i want it to return true by setting values of those two JSO's. Basically i want to compare between two JSO.. By your approach how can i be able to compare it can you please modify existing code and send me back . that would be great!
    Last edited by VIPStephan; 02-22-2012 at 12:28 PM. Reason: wrapped code in BB tags

  • #4
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,775
    Thanks
    6
    Thanked 1,021 Times in 994 Posts
    Sorry, the last post was hidden in the moderation queue. However, this is JAVA, not JavaScript. Keep that in mind for the next time, sur_7887. I’m moving it to the correct forum.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Since JSObject is not a built in interface, you'll need to modify the JSObject interface and add an extension of type Comparable<JSObject> to it.
    This will force you to implement CompareTo so you can compare two objects of the same type and return 0 if its equal, < 0 if "other" is < "this", and > 0 if "this" is > "other". You need to write what it compares of course.
    This can then be used in any collection type to sort, or used manually with jsobject1.compareTo(jsobject2). Not sure exactly what it will do if you use it in anonymous inner classes with a collection, but I presume it will work.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •