当前位置:网站首页>深度比较两个对象是否相同
深度比较两个对象是否相同
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
}
边栏推荐
- (2022牛客多校四)K-NIO‘s Sword(思维)
- Swastika line-by-line parsing and realization of the Transformer, and German translation practice (a)
- 第5章——以程序方式处理MySQL数据表的数据
- 2022年湖南工学院ACM集训第六次周测题解
- Selenium: element positioning
- About making a progress bar for software initialization for Qt
- 字符中的第一个唯一字符
- MySQL-DML语言-数据库操作语言-insert-update-delete-truncate
- Selenium:简介
- pytorch、tensorflow对比学习—功能组件(优化器、评估指标、Module管理)
猜你喜欢

About making a progress bar for software initialization for Qt

Robot_Framework:关键字

(2022牛客多校四)A-Task Computing (排序+动态规划)

Robot_Framework:常用内置关键字

pytroch、tensorflow对比学习—使用GPU训练模型

pytroch、tensorflow对比学习—专栏介绍

可持久化线段树

MySQL-DML language-database operation language-insert-update-delete-truncate

HJS-DE1/2时间继电器

Induction jian hai JustFE 2022/07/29 team, I learned the efficient development summary (years)
随机推荐
NDK does not contain any platforms问题解决
Induction jian hai JustFE 2022/07/29 team, I learned the efficient development summary (years)
冲刺金九银十,Android开发面试(内含面试资料|面试题|源码)
4D line-by-line analysis and implementation of Transformer, and German translation into English (3)
Robot_Framework: commonly used built-in keywords
万字逐行解析与实现Transformer,并进行德译英实战(一)
报错:AttributeError: module ‘matplotlib’ has no attribute ‘figure’
Selenium:弹窗处理
torch
可持久化线段树
I met a shell script
pytroch、tensorflow对比学习—使用GPU训练模型
ModuleNotFoundError: No module named 'tensorflow.keras' error message solution
第6章——数据库的安全性
MySQL-数据操作-分组查询-连接查询-子查询-分页查询-联合查询
[MySQL] 多表查询
请求/响应拦截器写法
JWL-11/2-99.9A电流继电器
NDK does not contain any platforms problem solving
PAT serie b write the number 1002