当前位置:网站首页>深度比较两个对象是否相同
深度比较两个对象是否相同
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
}
边栏推荐
猜你喜欢
Selenium: Popup Handling
(2022牛客多校四)K-NIO‘s Sword(思维)
可持久化线段树
MySQL-数据操作-分组查询-连接查询-子查询-分页查询-联合查询
Selenium:弹窗处理
【MySQL必知必会】 表的优化 | 充分利用系统资源
Robot growth in China
AspNet.WebApi.Owin custom Token request parameters
The sword refers to Offer 68 - I. Nearest Common Ancestor of Binary Search Trees
The solution to the inconsistency between the PaddleX deployment inference model and the GUI interface test results
随机推荐
matplotlib pyplot
Robot_Framework:关键字
56:第五章:开发admin管理服务:9:开发【文件上传到,MongoDB的GridFS中,接口】;(把文件上传到GridFS的SOP)
Induction jian hai JustFE 2022/07/29 team, I learned the efficient development summary (years)
【音视频】srs直播平台搭建
万字逐行解析与实现Transformer,并进行德译英实战(二)
Selenium:鼠标、键盘事件
(more than 2022 cattle school four) A - Task Computing + dynamic programming (sort)
WPF项目-按着键盘方向键,移动格子盒子效果
state compressed dp
Lawyer Interpretation | Guns or Roses?Talking about Metaverse Interoperability from the Battle of Big Manufacturers
Selenium:操作JS
pytorch、tensorflow对比学习—功能组件(优化器、评估指标、Module管理)
[MySQL] 多表查询
报错:AttributeError: module ‘matplotlib’ has no attribute ‘figure’
混合型界面:对话式UI的未来
A,H,K,N
Causes and solutions of lock table
Check控件
2022.7.27好题选讲