如何创建一个函数来检查两个对象是否具有相同的属性?[javascript]

mkshixfv  于 2023-01-24  发布在  Java
关注(0)|答案(4)|浏览(130)

如果两个对象中的所有属性都相同,我如何创建一个返回true的函数?我已经编写了下面的代码,如果至少有一个属性相同,它将返回true。但是,如果其中一个属性不同,它应该返回false。请记住,我正在学习JavaScript...谢谢!

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;
}
jm81lzqq

jm81lzqq1#

检查两个对象是否相等。

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))
5n0oy7gb

5n0oy7gb2#

例如,实现这一目标有多种途径。
使用JSON.stringfy的方法1

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

使用递归的方法2

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]));
}

使用Object.keys()和loop的组合的方法3

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;
}

请注意,方法1和方法2也适用于嵌套属性,而方法3仅适用于顶级属性。
希望能有所帮助。

gzjq41n4

gzjq41n43#

下面是一个函数示例,该函数比较两个对象,如果它们的所有属性都相同,则返回true:'

function areObjectsEqual(obj1, obj2) {
    // get the properties of each object
    const obj1Props = Object.getOwnPropertyNames(obj1);
    const obj2Props = Object.getOwnPropertyNames(obj2);
    // check if the number of properties is different
    if (obj1Props.length !== obj2Props.length) {
        return false;
    }
    // iterate over each property and compare the values
    for (let i = 0; i < obj1Props.length; i++) {
        const propName = obj1Props[i];
        if (obj1[propName] !== obj2[propName]) {
            return false;
        }
    }
    return true;
}

'

ehxuflar

ehxuflar4#

你可以比较排序键数组。我的解决方案的一部分是基于这个问题: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

相关问题