JavaScript 如何比较两个对象以确定第一个对象是否包含与第二个对象的等效属性值
在本文中,我们将学习如何比较两个对象以确定第一个对象是否包含与第二个对象的等效属性值。在JavaScript中,比较两个对象的值涉及检查它们是否具有相同的属性和对应的值。给定两个对象 obj1 和 obj2 ,我们的任务是检查 obj1 是否包含 obj2 的所有属性值。
Input : obj1: { name: "John", age: 23; degree: "CS" }
obj2: {age: 23, degree: "CS"}
Output: true
Input : obj1: { name: "John", degree: "CS" }
obj2: {name: "Max", age: 23, degree: "CS"}
Output : false
为了解决这个问题,我们采用以下方法。
- 使用for..in循环
- 使用Object.keys()和Array.every()
- 使用JSON.stringify()
- 使用自定义函数
- 使用Object.entries()
我们将通过示例讲解上述所有方法及其基本实现。
方法1:使用for..in循环
这是一种简单的方法来解决这个问题。在这种方法中,我们使用for..in循环迭代obj2,在每次迭代时检查两个对象的当前键是否不相等,如果不相等则返回false,否则在循环完成后返回true。
示例:在这个示例中,我们使用了上述解释的方法。
// Define the first object
let obj1 = {
name: "John",
age: 23,
degree: "CS"
}
// Define the second object
let obj2 = {
age: 23,
degree: "CS"
}
// Define the function check
function check(obj1, obj2) {
// Iterate the obj2 using for..in
for (key in obj2) {
// Check if both objects do
// not have the equal values
// of same key
if (obj1[key] !== obj2[key]) {
return false;
}
}
return true
}
// Call the function
console.log(check(obj1, obj2))
输出
true
方法2:使用Object.keys()和Array.every()
在这个方法中,我们通过使用 Object.keys()方法 创建一个包含obj2所有键的数组,然后使用 Array.every()方法 检查obj2的所有属性是否都等于obj1。
示例: 在这个示例中,我们使用上述解释的方法。
// Define the first object
let obj1 = {
name: "John",
age: 23,
degree: "CS"
}
// Define the Second object
let obj2 = {
age: 23,
degree: "CS"
}
// Define the function check
function check(obj1, obj2) {
return Object
// Get all the keys in array
.keys(obj2)
.every(val => obj1.hasOwnProperty(val)
&& obj1[val] === obj2[val])
}
// Call the function
console.log(check(obj1, obj2))
输出
true
方法3:使用JSON.stringify()
在这种方法中,我们使用JSON.stringify()将两个对象转换为JSON字符串,然后使用===
运算符来比较字符串的严格相等性。如果这两个对象具有相同的属性和值(无论顺序如何),字符串将相等,函数将返回true。
示例: 在这个例子中,areObjectsEqual函数通过将两个对象obj1和obj2转换为JSON字符串进行比较。输出结果为true,因为这两个对象具有相同的属性和值。
function areObjectsEqual(obj1, obj2) {
const stringifiedObj1 = JSON.stringify(obj1);
const stringifiedObj2 = JSON.stringify(obj2);
return stringifiedObj1 === stringifiedObj2;
}
let obj1 = {
name: "John",
age: 23,
degree: "CS"
};
let obj2 = {
name: "John",
age: 23,
degree: "CS"
};
console.log(areObjectsEqual(obj1, obj2));
输出
true
方法4:使用自定义函数
在这种方法中,我们定义了areObjectsEqual函数,用于比较两个对象obj1和obj2。它首先检查两个对象键的数量是否相同;如果不同,就返回false。
示例: 在这个示例中,我们使用了一个自定义函数areObjectsEqual,示例比较了两个对象obj1和obj2,返回true,因为它们具有相同的属性和值。
function areObjectsEqual(obj1, obj2) {
const keys1 = Object.keys(obj1);
const keys2 = Object.keys(obj2);
if (keys1.length !== keys2.length) {
return false;
}
for (const key of keys1) {
if (obj1[key] !== obj2[key]) {
return false;
}
}
return true;
}
let obj1 = {
name: "John",
age: 23,
degree: "CS"
};
let obj2 = {
name: "John",
age: 23,
degree: "CS"
};
console.log(areObjectsEqual(obj1, obj2));
输出
true
方法5:使用 Object.entries()
在这种方法中,我们使用 Object.entries() 来从对象 obj1 和 obj2 获取键值对的数组。然后,我们比较这些数组的长度。如果它们不相等,则意味着对象具有不同数量的属性,因此我们立即返回 false。
示例: 在这个示例中,我们使用了 Object.entries(),该示例比较了 obj1 和 obj2,返回 true,因为两者都具有相同的属性和值。
function areObjectsEqual(obj1, obj2) {
const entries1 = Object.entries(obj1);
const entries2 = Object.entries(obj2);
if (entries1.length !== entries2.length) {
return false;
}
for (const [key, value] of entries1) {
if (!obj2.hasOwnProperty(key) || obj2[key] !== value) {
return false;
}
}
return true;
}
let obj1 = {
name: "John",
age: 23,
degree: "CS"
};
let obj2 = {
name: "John",
age: 23,
degree: "CS"
};
console.log(areObjectsEqual(obj1, obj2));
输出
true