Questions tagged [object-comparison]
56 questions
1245
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
- 19,078
- 10
- 38
- 69
680
votes
23 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
- 17,648
- 25
- 70
- 108
90
votes
5 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
- 10,523
- 17
- 77
- 117
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
- 23,833
- 33
- 104
- 154
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:
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
- 66,219
- 52
- 237
- 318
23
votes
9 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
- 26,790
- 72
- 224
- 390
15
votes
3 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
- 45,122
- 42
- 127
- 254
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
- 965
- 1
- 11
- 18
7
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
- 147
- 1
- 1
- 10
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
- 10,682
- 4
- 38
- 58
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
- 19,104
- 14
- 65
- 90
5
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
- 10,413
- 12
- 81
- 120
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
- 231
- 1
- 2
- 8
4
votes
1 answer
Fuzzy deep object comparison
I have a method that returns a huge object with a lot of fields. Something like this:
{
Success: true,
Timestamp: "07.03.2014",
Items:
[
{
Name: "A",
Price: 13.37,
OtherData: 123
…
Impworks
- 2,314
- 3
- 23
- 47