function areObjectsEqual(obj1, obj2) {
// 获取两个对象的属性名
const keys1 = Object.keys(obj1);
const keys2 = Object.keys(obj2);
// 比较属性数量是否一致
if (keys1.length !== keys2.length) {
return false;
}
// 比较属性名是否一致
for (let key of keys1) {
if (!keys2.includes(key)) {
return false;
}
}
// 比较属性值
for (let key of keys1) {
const value1 = obj1[key];
const value2 = obj2[key];
if (typeof value1 === "object" && typeof value2 === "object") {
// 递归比较对象的属性值
if (!areObjectsEqual(value1, value2)) {
return false;
}
} else if (value1 !== value2) {
return false;
}
}
return true;
}