How do I check if an object has a key in JavaScript? [duplicate]

This question already has answers here: How do I check if an object has a specific property in JavaScript? (30 answers) Closed 8 years ago. Which is the right thing to do? if (myObj[‘key’] == undefined) or if (myObj[‘key’] == null) or if (myObj[‘key’]) 2 Try the JavaScript in operator. if (‘key’ in myObj) And … Read more

What is the most efficient way to deep clone an object in JavaScript?

This question’s answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions. What is the most efficient way to clone a JavaScript object? I’ve seen obj = eval(uneval(o)); being used, but that’s non-standard and only supported by Firefox. I’ve done things like obj = … Read more

How do I copy an object in Java?

Create a copy constructor: class DummyBean { private String dummy; public DummyBean(DummyBean another) { this.dummy = another.dummy; // you can access } } Every object has also a clone method which can be used to copy the object, but don’t use it. It’s way too easy to create a class and do improper clone method. … Read more

Java Undefined Object

By “undefined object as a parameter”, I assume you mean that you’re looking to write a function that doesn’t specify the type of the object in the function declaration, allowing you to only have one function. This can be done with generics. Instead of: static void func(String str) { System.out.println(“The string is: “+str); } static … Read more

How to identify object types in java

You forgot the .class: if (value.getClass() == Integer.class) { System.out.println(“This is an Integer”); } else if (value.getClass() == String.class) { System.out.println(“This is a String”); } else if (value.getClass() == Float.class) { System.out.println(“This is a Float”); } Note that this kind of code is usually the sign of a poor OO design. Also note that comparing the … Read more

How to sort an array of objects in Java?

You have two ways to do that, both use the Arrays utility class Implement a Comparator and pass your array along with the comparator to the sort method which take it as second parameter. Implement the Comparable interface in the class your objects are from and pass your array to the sort method which takes only one parameter. Example class Book implements Comparable<Book> { public … Read more