1

i am trying to compare two objects. The ages are different but still says they are same.. here is my code

var personA =
    {
        name: "Josh Kloss",
        age: 33
    }
var personmB =
{
    name: 'Josh Kloss',
    age: 43
}

function compareTwoPeople(a, b) {

    var person1 = Object.keys(a);
    var person2 = Object.keys(b);
    if (person1.length !== person2.length) {
        console.log("They are not same");
    }
    else {
        for (var i = 0; i < person1.length; i++) {
            if (person1[i] === person2[i]) {
                console.log("They are  same");
            }
        }
    }

}

compareTwoPeople(personA, personmB);

How can i compare these two object... Thanks.

Cloudboy22
  • 1,496
  • 5
  • 21
  • 39

4 Answers4

0

You're looking up person1[i] which will be [ 'name', 'age' ][0], which is 'name', the key name. It's the same for both objects. The quick solution to your question is:

if (a[person1[i]] !== b[person2[i]]) {
    console.log("They are not the same");
    return;
}

However this is not a great equality test, because your objects could both have two keys, but different keys.

Andy Ray
  • 30,372
  • 14
  • 101
  • 138
0

Close, just need to tidy up the comparison a little bit to get at the object value using the key and the iterator.

var personA =
    {
        name: "Josh Kloss",
        age: 33
    };
var personB =
{
    name: 'Josh Kloss',
    age: 43
};

function compare(a, b) {
  var aKeys = Object.keys(a),
      bKeys = Object.keys(b);

  if(aKeys.length != bKeys.length) return false;

  for(var i = 0; i < aKeys.length; i++) {
    if(a[aKeys[i]] != b[bKeys[i]]) return false;
  }

  return true;

}

compare(personA, personB);
mariocatch
  • 8,305
  • 8
  • 50
  • 71
0

You might consider using Array.prototype.every for the property value comparison part. It's efficient as it returns false at the first mismatch rather than going over all the properties when it doesn't have to:

var personA = {name: "Josh Kloss", age: 33};
var personB = {name: 'Josh Kloss', age: 43};

// Return true if objects a and b have the same properties
// with the same values. Otherwise, false.
function compareTwoPeople(a, b) {

  var person1 = Object.keys(a);
  var person2 = Object.keys(b);

  // Use first test to initialise result
  var result = person1.length === person2.length;

  // If passed the first test, do the second
  if (result) {

    // Use every to test each value, returns false at first mismatch
    result = person1.every(function(prop){
      return a[prop] === b[prop];
    });
  }
  return result;
}

document.write('Are they the same? ' + compareTwoPeople(personA, personB));
RobG
  • 142,382
  • 31
  • 172
  • 209
-1

you can compare by going through each properties,

var personA =
    {
        name: "Josh Kloss",
        age: 33
    };

var personB =
{
    name: 'Josh Kloss',
    age: 43
};

function getObjectSize(obj){
    var size = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) size++;
    }
    return size;
}

function isObjectsEqual(a, b){

    //check objects has equal length
    if(getObjectSize(a) !== getObjectSize(b)){
        return false;
    }

    //check properties are equal
    for (var key in a) {

        if (b.hasOwnProperty(key)){

            if(a[key] != b[key]){
                return false;
            }
        }
        else{
            return false;
        }
    }

    return true;
}

isObjectsEqual(personA, personB); //test function
Azad
  • 5,144
  • 4
  • 28
  • 56