在检查新创建的对象是否与数组中的对象相同时,可以使用以下方法:
以下是一个示例代码,演示如何检查新创建的对象是否与数组中的对象相同:
function checkObjectEquality(newObject, array) {
for (let i = 0; i < array.length; i++) {
let isEqual = true;
const currentObject = array[i];
// Compare properties of objects
for (let key in currentObject) {
if (currentObject.hasOwnProperty(key) && newObject.hasOwnProperty(key)) {
if (currentObject[key] !== newObject[key]) {
isEqual = false;
break;
}
} else {
isEqual = false;
break;
}
}
// Check if all properties are equal
if (isEqual) {
return true;
}
}
return false;
}
// Example usage
const array = [
{ name: 'John', age: 25 },
{ name: 'Jane', age: 30 },
{ name: 'Bob', age: 35 }
];
const newObject = { name: 'Jane', age: 30 };
const isSame = checkObjectEquality(newObject, array);
console.log(isSame); // Output: true
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云