当前位置:网站首页>盘点JS判断空对象的几大方法
盘点JS判断空对象的几大方法
2022-07-07 10:07:00 【InfoQ】
知识准备
Object.keys
Object.keys() var obj = {
name: "cxy",
age: "19"
};
var objArray = Object.getOwnPropertyNames(obj);
console.log(objArray)

objArrayObject.getOwnPropertyNames
Object.getOwnPropertyNames()Object.getOwnPropertyNames()Object.keys()可枚举属性
enumerableobj.name = "cxy"enumerableturefalseforObject.keys()JSON.stringify()Object.getOwnPropertyNamesObject.keys()definePropertyagefalsevar stuObj = {
name: "cxy"
}
Object.defineProperty(stuObj, 'age', {
value: "18",
enumerable: false
});
console.log(Object.keys(stuObj))
console.log(Object.getOwnPropertyNames(stuObj))

hasOwnProperty
hasOwnProperty() var stuObj = {
name: "cxy"
}
console.log(stuObj.hasOwnProperty('name'))

hasOwnProperty()falsetoString盘点判空方法
JSON.stringify
判空
JSON.stringifylet obj = {
name: "cxy"
}
console.log(JSON.stringify(obj) == '{}')
for in
判空
for infalseturelet forNull = (items) => {
for (let item in items) {
return false
}
return true
}
Object.getOwnPropertyNames
判空
Object.getOwnPropertyNameslengthlet stuArray = Object.getOwnPropertyNames(obj)
console.log(stuArray.length === 0)
Object.keys()
判空
let stuArray = Object.getOwnPropertyNames(obj)
console.log(stuArray.length === 0)
hasOwnProperty
判空
hasOwnPropertyfalselet forNull = (items) => {
for (let item in items) {
if(items.hasOwnProperty(item)){
return false
}
}
return true
}
边栏推荐
- 相机标定(1): 单目相机标定及张正友标定基本原理
- Completion report of communication software development and Application
- [texture feature extraction] LBP image texture feature extraction based on MATLAB local binary mode [including Matlab source code 1931]
- Rationaldmis2022 advanced programming macro program
- Sonar:cognitive complexity
- [filter tracking] strapdown inertial navigation pure inertial navigation solution matlab implementation
- In my limited software testing experience, a full-time summary of automation testing experience
- 111.网络安全渗透测试—[权限提升篇9]—[Windows 2008 R2内核溢出提权]
- 让数字管理好库存
- Solve the problem that vscode can only open two tabs
猜你喜欢

Time bomb inside the software: 0-day log4shell is just the tip of the iceberg
![[data clustering] realize data clustering analysis based on multiverse optimization DBSCAN with matlab code](/img/83/0652e3138b87a4741dd8261a24d1e8.png)
[data clustering] realize data clustering analysis based on multiverse optimization DBSCAN with matlab code
![[filter tracking] strapdown inertial navigation pure inertial navigation solution matlab implementation](/img/14/6e440f3c4e04d9b322f0c3f43e213c.png)
[filter tracking] strapdown inertial navigation pure inertial navigation solution matlab implementation

HCIA复习整理
![[texture feature extraction] LBP image texture feature extraction based on MATLAB local binary mode [including Matlab source code 1931]](/img/65/bf1d0f82878a49041e8c2b3a84bc15.png)
[texture feature extraction] LBP image texture feature extraction based on MATLAB local binary mode [including Matlab source code 1931]

Swiftui tutorial how to realize automatic scrolling function in 2 seconds
![110. Network security penetration test - [privilege promotion 8] - [windows sqlserver xp_cmdshell stored procedure authorization]](/img/62/1ec8885aaa2d4dca0e764b73a1e2df.png)
110. Network security penetration test - [privilege promotion 8] - [windows sqlserver xp_cmdshell stored procedure authorization]
![111.网络安全渗透测试—[权限提升篇9]—[Windows 2008 R2内核溢出提权]](/img/2e/da45198bb6fb73749809ba0c4c1fc5.png)
111.网络安全渗透测试—[权限提升篇9]—[Windows 2008 R2内核溢出提权]

《通信软件开发与应用》课程结业报告

清华姚班程序员,网上征婚被骂?
随机推荐
Steps of redis installation and self startup configuration under CentOS system
When sink is consumed in mysql, the self incrementing primary key has been set in the database table. How to operate in Flink?
111.网络安全渗透测试—[权限提升篇9]—[Windows 2008 R2内核溢出提权]
[data clustering] realize data clustering analysis based on multiverse optimization DBSCAN with matlab code
Onedns helps college industry network security
【滤波跟踪】捷联惯导纯惯导解算matlab实现
110. Network security penetration test - [privilege promotion 8] - [windows sqlserver xp_cmdshell stored procedure authorization]
正在运行的Kubernetes集群想要调整Pod的网段地址
Flet教程之 18 Divider 分隔符组件 基础入门(教程含源码)
powershell cs-UTF-16LE编码上线
UP Meta—Web3.0世界创新型元宇宙金融协议
2022 8th "certification Cup" China University risk management and control ability challenge
SwiftUI Swift 内功之如何在 Swift 中进行自动三角函数计算
Talk about SOC startup (VI) uboot startup process II
The Oracle message permission under the local Navicat connection liunx is insufficient
《论文阅读》Neural Approaches to Conversational AI(1)
Flet教程之 19 VerticalDivider 分隔符组件 基础入门(教程含源码)
108. Network security penetration test - [privilege escalation 6] - [windows kernel overflow privilege escalation]
What are the technical differences in source code anti disclosure
核舟记(一):当“男妈妈”走进现实,生物科技革命能解放女性吗?