当前位置:网站首页>深度比较两个对象是否相同
深度比较两个对象是否相同
2022-08-01 05:21:00 【時間不夠以後】
/** * 深度比较两个对象是否相同 * @param {Object} oldData * @param {Object} newData */
export const equals2Obj = (oldData, newData) => {
// 类型为基本类型时,如果相同,则返回true
if (oldData === newData) return true
if (isObject(oldData) && isObject(newData) && Object.keys(oldData).length === Object.keys(newData).length) {
// 类型为对象并且元素个数相同
// 遍历所有对象中所有属性,判断元素是否相同
for (const key in oldData) {
if (Object.prototype.hasOwnProperty.call(oldData, key)) {
if (!equals2Obj(oldData[key], newData[key])) {
// 对象中具有不相同属性 返回false
return false
}
}
}
} else if (isArray(oldData) && isArray(newData) && oldData.length === newData.length) {
// 类型为数组并且数组长度相同
for (let i = 0, length = oldData.length; i < length; i++) {
if (!equals2Obj(oldData[i], newData[i])) {
// 如果数组元素中具有不相同元素,返回false
return false
}
}
} else {
// 其它类型,均返回false
return false
}
// 走到这里,说明数组或者对象中所有元素都相同,返回true
return true
}
边栏推荐
猜你喜欢
MySQL-数据操作-分组查询-连接查询-子查询-分页查询-联合查询
剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
y83. Chapter 4 Prometheus Factory Monitoring System and Actual Combat -- Advanced Prometheus Alarm Mechanism (14)
力扣(LeetCode)212. 单词搜索 II(2022.07.31)
从离线到实时对客,湖仓一体释放全量数据价值
Selenium:操作Cookie
字符中的第一个唯一字符
(2022牛客多校四)H-Wall Builder II(思维)
leetcode43 字符串相乘
PAT serie b write the number 1002
随机推荐
【音视频】srs直播平台搭建
uva12326
Selenium: upload and download files
Robot_Framework: Assertion
NUMPY
MySQL-Data Operation-Group Query-Join Query-Subquery-Pagination Query-Joint Query
2022.7.27好题选讲
挑战52天背完小猪佩奇(第01天)
About making a progress bar for software initialization for Qt
Selenium:弹窗处理
用控件当画笔获得bitmap代码记录
【翻译】确保云原生通信的安全:从入口到服务网及更远的地方
Asynchronous reading and writing of files
LeetCode 1189. “气球” 的最大数量
4D line-by-line analysis and implementation of Transformer, and German translation into English (3)
state compressed dp
Causes and solutions of lock table
Hunan institute of technology in 2022 ACM training sixth week antithesis
USB3.0:VL817Q7-C0的LAYOUT指南(三)
(2022牛客多校四)K-NIO‘s Sword(思维)