Questions tagged [object-comparison]
66 questions
1458
votes
10 answers
Object comparison in JavaScript
What is the best way to compare objects in JavaScript?
Example:
var user1 = {name : "nerd", org: "dev"};
var user2 = {name : "nerd", org: "dev"};
var eq = user1 == user2;
alert(eq); // gives false
I know that two objects are equal if they refer to…

spankmaster79
- 21,555
- 10
- 42
- 73
768
votes
26 answers
What is the difference between == and equals() in Java?
I wanted to clarify if I understand this correctly:
== is a reference comparison, i.e. both objects point to the same memory location
.equals() evaluates to the comparison of values in the objects

brainydexter
- 19,826
- 28
- 77
- 115
98
votes
6 answers
Checking object equality in Jasmine
Jasmine has built-in matchers toBe and toEqual. If I have an object like this:
function Money(amount, currency){
this.amount = amount;
this.currency = currency;
this.sum = function (money){
return new Money(200, "USD");
…

Dan
- 11,077
- 20
- 84
- 119
63
votes
7 answers
Python: Why does ("hello" is "hello") evaluate as True?
Why does "hello" is "hello" produce True in Python?
I read the following here:
If two string literals are equal, they have been put to same
memory location. A string is an immutable entity. No harm can
be done.
So there is one and only one…

Deniz Dogan
- 25,711
- 35
- 110
- 162
45
votes
2 answers
How to compare two object variables in EL expression language?
I am creating a drop down list of all languages. The default language selection for the list will be determined by information added by the user:
39
votes
11 answers
How can I do a shallow comparison of the properties of two objects with Javascript or lodash?
Is there a way I can do a shallow comparison that will not go down and compare the contents of objects inside of objects in Javascript or lodash? Note that I did check lodash, but it appears to perform a deep comparison which I don't want to do.
var…

Samantha J T Star
- 30,952
- 84
- 245
- 427
34
votes
9 answers
How do I compare two Integers?
I have to compare two Integer objects (not int). What is the canonical way to compare them?
Integer x = ...
Integer y = ...
I can think of this:
if (x == y)
The == operator only compares references, so this will only work for lower integer…

Daniel Rikowski
- 71,375
- 57
- 251
- 329
22
votes
4 answers
Is it fine to use JSON.stringify for deep comparisons and cloning?
After attempting several implementations for deep comparison and copying for JSON-serializable objects, I've noticed the fastest often are just:
function deep_clone(a){
return JSON.parse(JSON.stringify(a));
};
function is_equal(a,b){
return…

MaiaVictor
- 51,090
- 44
- 144
- 286
14
votes
2 answers
How does in_array check if an object is in an array of objects?
Does in_array() do object comparison where it checks that all attributes are the same?
What if $obj1 === $obj2, will it just do pointer comparison instead?
I'm using an ORM, so I'd rather loop over the objects testing if $obj1->getId() is already in…

yellottyellott
- 975
- 1
- 11
- 18
9
votes
1 answer
Javascript: Deep Comparison
I was checking this question Javascript Deep Comparison
The solution of the question asker did not convince me so I tried to analyze the problem and came up with that
var obj = {here: 2};
console.log(deepEqual(obj, obj));
// →…

zenwaichi
- 187
- 1
- 1
- 10
8
votes
2 answers
Python: Comparing two JSON objects in pytest
I have an API that returns this JSON response
{
"message": "Staff name and password pair not match",
"errors": {
"resource": "Login",
"field": "staff_authentication",
"code": "invalid",
"stack_trace": null
…

Hanxue
- 12,243
- 18
- 88
- 130
6
votes
2 answers
Java Integer pool. Why?
I've read everywhere that when you define an Integer between -128 to 127 in Java, instead of creating a new object it returns an object already created.
I don't see any point of doing this other than letting newbie programmers compare Integer…

Jorge Fuentes González
- 11,568
- 4
- 44
- 64
6
votes
3 answers
Is "==" in Ruby always value equality?
Sorry if duplicated (I didn't find it)
This is only to confirm that Ruby's operator == performs always an equality comparison.
I.e.
a == b
compares a's value against b's value, instead than, like Java, whether they point to the same object in…

cibercitizen1
- 20,944
- 16
- 72
- 95
5
votes
3 answers
Does the equals method work with objects? If so, how?
I have a program that is zoo and in the zoo there are branched subgroups of animals that are reptiles. When I do an equals method the main program compiles and it runs. I'm confused how does java know to use the equals method if I'm comparing…

Ivan_Stepul
- 229
- 1
- 2
- 8
4
votes
2 answers
Python pickle: pickled objects are not equal to source objects
I think this is expected behaviour but want to check and maybe find out why, as the research I have done has come up blank
I have a function that pulls data, creates a new instance of my custom class, then appends it to a list. The class just…

Richard
- 179
- 1
- 6