当前位置:网站首页>判断两个对象的值是否都相等
判断两个对象的值是否都相等
2022-07-29 10:43:00 【大佩梨】
对象不可以直接==判断是否相等,由于对象是由键值对构成的,那要判断对象相等,只要判断两个对象的键、值是否都相同,如果相同就代表它们相等,反之不相等。
思路:
使用Object.getOwnPropertyNames()方法返回一个由指定对象的所有自身属性的属性名(包括不可枚举属性但不包括Symbol值作为名称的属性)组成的数组。
比对键名数组的长度是否相等,不相等则返回false。相等进行下一步。
比对键名对应的键值是否相等。
let onj1 = {
a: 1,
b: 2
}
let onj2 = {
b: 2,
a: 1
}
function isObjectValueEqual(a, b) {
var aProps = Object.getOwnPropertyNames(a);//得到aProps值为['a','b']
var bProps = Object.getOwnPropertyNames(b);
if (aProps.length !== bProps.length) {
return false;
}
for (var i = 0; i < aProps.length; i++) {
var propName = aProps[i];
var propA = a[propName];
var propB = b[propName];
if (propA !== propB) {
return false;
}
}
return true;
}
console.log(isObjectValueEqual(onj1, onj2)) // true
边栏推荐
- GPO: using PowerShell scripts in start/logon
- Use R-Pack skimr to collect the beautiful display of President measurement
- The server
- Learning R language these ebooks are enough!
- [configuration related]
- [dark horse morning post] Youxian responded to the dissolution every day, and many places have been unable to place orders; Li Bin said that Wei Lai will produce a mobile phone every year; Li Ka Shing
- 【Unity,C#】Character键盘输入转向与旋转
- Conference OA project - my approval
- Start from scratch blazor server (3) -- add cookie authorization
- 重磅 | 开放原子校源行活动正式启动
猜你喜欢

数据可视化设计指南(信息图表篇)

QT工程基本构建

周鸿祎:360是世界上最大的安全大数据公司

Second handshake?? Three waves??

Understand what a binary tree is (types, traversal methods, definitions of binary trees)

Kunlunbase instruction manual (III) data import & synchronization

The 2022 open atom global open source summit opened in Beijing

Pytorch 入门

Attachment of text of chenjie Report

Static resource mapping
随机推荐
Second handshake?? Three waves??
Open source, compliance escort! 2022 open atom global open source summit open source compliance sub forum is about to open
The server
LeetCode二叉树系列——144.二叉树的前序遍历
[IVI] 17.1 debugging pit FAQ (compilation)
[paper reading] i-bert: integer only Bert quantification
NUMA architecture CPU API change summary
软件测试干货
从零开始Blazor Server(3)--添加cookie授权
Error: Protobuf syntax version should be first thing in file
How beautiful can VIM be configured?
Research on the realization of linear gradient circular progress bar
Review of the 16th issue of HMS core discovery | play with the new "sound" state of AI with tiger pier
[reading notes] the way of enterprise IT architecture transformation Alibaba's China Taiwan strategic thinking and Architecture Practice
主子仓库都修改,如何进行同步?
Kunlunbase instruction manual (II) best practices for peer-to-peer deployment
Drawing box and ellipse of WPF screenshot control (IV) "imitating wechat"
[unity, C #] character keyboard input steering and rotation
Learning R language these ebooks are enough!
ADB shell WM command and usage: