-1

How can I create a function that return true if all the properties in two objects are the same? I have come up with the following code which will return true if at least one of the properties are the same. However if one of the properties are different, it should return false. Bear in mind that I'm in the learning process of JavaScript...Thank you!

function BuildAddress(street, city, zipCode) {
  this.street = street;
  this.city = city;
  this.zipCode = zipCode;
}

const address1 = new BuildAddress('a', 'b', 101);
const address2 = new BuildAddress('a', 'b', 101);

function areEqual(address1, address2) {
  for (let key in address1)
    for (let value in address2)
  if (address1[key] === address2[value]) return true;
  return false;
}
  • Note: Since `BuildAddress` is used as constructor function it should be named `Address`. The latter describes the type one does expect from invoking the constructor via `new`, the the former sounds like a factory function which creates `address` objects via simply invoking it (without `new`). – Peter Seliger Jan 23 '23 at 13:01
  • Does the OP want to implement a comparison function which compares just two `Address` types? Or does the OP want to come up with a generic comparison approach/solution? – Peter Seliger Jan 23 '23 at 13:04
  • As for the latter, the OP might have a look into ... [an approach which implements a `isDeepDataStructureEquality` comparison function](https://stackoverflow.com/questions/71015428/how-to-get-the-intersection-of-two-sets-while-recognizing-equal-set-values-items/71016510#71016510) – Peter Seliger Jan 23 '23 at 13:11

3 Answers3

0

to check two object is equal or not.

const objectsEqual = (o1, o2) => 
      typeof o1 === 'object' && Object.keys(o1).length > 0 ? 
        Object.keys(o1).length === Object.keys(o2).length 
          && Object.keys(o1).every(p => objectsEqual(o1[p], o2[p]))
        : o1 === o2;


function BuildAddress(street, city, zipCode) {
 this.street = street;
 this.city = city;
 this.zipCode = zipCode;
}

const address1 = new BuildAddress('a', 'b', 101);
const address2 = new BuildAddress('a', 'b', 101);

console.log(objectsEqual(address1, address2))
0

There are multiple ways of achieving it, for example.

Method 1 using JSON.stringfy

function compareObjects(obj1, obj2) {
  return JSON.stringify(obj1) === JSON.stringify(obj2);
}

Method 2 using recursion

function isEqual(a, b) {
    if (a === b) return true;
    if (a instanceof Date && b instanceof Date) return a.getTime() === b.getTime();
    if (!a || !b || (typeof a !== 'object' && typeof b !== 'object')) return a === b;
    if (a.prototype !== b.prototype) return false;
    let keys = Object.keys(a);
    if (keys.length !== Object.keys(b).length) return false;
    return keys.every(k => isEqual(a[k], b[k]));
}

Method 3 using combination of Object.keys() and loop

function compareObjects(obj1, obj2) {
  const obj1Keys = Object.keys(obj1);
  const obj2Keys = Object.keys(obj2);

  if (obj1Keys.length !== obj2Keys.length) {
    return false;
  }

  for (let i = 0; i < obj1Keys.length; i++) {
    if (obj1[obj1Keys[i]] !== obj2[obj1Keys[i]]) {
      return false;
    }
  }

  return true;
}

Note that Method 1 and Method 2 will work for nested properties too, while Method 3 will work only for top level properties.

Hope that helps.

thug_
  • 893
  • 2
  • 11
  • 31
  • 1
    JSON string based comparison works exactly for e.g. types created by the OP's `BuildAddress` function. Such a comparison already fails for a different key-insertion order of otherwise two equal objects. – Peter Seliger Jan 23 '23 at 12:57
  • Good point! Using `JSON.stringify()` to compare objects can be problematic if the objects have properties with different key order but the values are the same. Order of keys in a JSON string is not guaranteed to be the same as the order of keys in the original object. – thug_ Jan 23 '23 at 14:33
-1

You can compare sorted keys array. Part of my solution is based on this question: How to check if two arrays are equal with JavaScript?

function BuildAddress(street, city, zipCode) {
    this.street = street;
    this.city = city;
    this.zipCode = zipCode;
}


function areEqual(a, b) {
    let arrA = Object.keys(a).sort()
    let arrB = Object.keys(b).sort()
    return arraysEqual(arrA, arrB)
}

function arraysEqual(a, b) {
    if (a === b) return true;
    if (a == null || b == null) return false;
    if (a.length !== b.length) return false;


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

const address1 = new BuildAddress('a', 'b', 101);
const address2 = new BuildAddress('a', 'b', 101);
const address3 = {spam:"foo"}

console.log(areEqual(address1, address2)) //true
console.log(areEqual(address1, address3)) //false

Semtex
  • 9
  • 3