当前位置:网站首页>盘点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
}
边栏推荐
- Swiftui tutorial how to realize automatic scrolling function in 2 seconds
- Sonar:Cognitive Complexity认知复杂度
- <No. 9> 1805. 字符串中不同整数的数目 (简单)
- 《通信软件开发与应用》课程结业报告
- Camera calibration (2): summary of monocular camera calibration
- Fleet tutorial 15 introduction to GridView Basics (tutorial includes source code)
- Talk about SOC startup (11) kernel initialization
- Basic introduction to the 16 tabs tab control in the fleet tutorial (the tutorial includes source code)
- 【最短路】Acwing1128信使:floyd最短路
- [data clustering] realize data clustering analysis based on multiverse optimization DBSCAN with matlab code
猜你喜欢
![[system design] index monitoring and alarm system](/img/8e/9c4c168f7f2b8e1f0786a5fe158544.png)
[system design] index monitoring and alarm system
![[full stack plan - programming language C] basic introductory knowledge](/img/6d/555ac6b80b015e0cdfb7ef0a234f9d.png)
[full stack plan - programming language C] basic introductory knowledge
![[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]

【滤波跟踪】基于matlab捷联惯导仿真【含Matlab源码 1935期】

【最短路】Acwing1128信使:floyd最短路

5V串口接3.3V单片机串口怎么搞?

Reasons for the failure of web side automation test

<No. 8> 1816. Truncate sentences (simple)

Test the foundation of development, and teach you to prepare for a fully functional web platform environment

Detailed explanation of debezium architecture of debezium synchronization
随机推荐
核舟记(一):当“男妈妈”走进现实,生物科技革命能解放女性吗?
Software design - "high cohesion and low coupling"
Half of the people don't know the difference between for and foreach???
CMU15445 (Fall 2019) 之 Project#2 - Hash Table 详解
相机标定(2): 单目相机标定总结
防红域名生成的3种方法介绍
Onedns helps college industry network security
Visual Studio 2019 (LocalDB)\MSSQLLocalDB SQL Server 2014 数据库版本为852无法打开,此服务器支持782版及更低版本
顶级域名有哪些?是如何分类的?
<No. 8> 1816. 截断句子 (简单)
[shortest circuit] acwing 1127 Sweet butter (heap optimized dijsktra or SPFA)
Zero shot, one shot and few shot
[data clustering] realize data clustering analysis based on multiverse optimization DBSCAN with matlab code
Flet教程之 18 Divider 分隔符组件 基础入门(教程含源码)
Explore cloud database of cloud services together
How to understand the clothing industry chain and supply chain
<No. 8> 1816. Truncate sentences (simple)
Le Cluster kubernets en cours d'exécution veut ajuster l'adresse du segment réseau du pod
[shortest circuit] acwing1128 Messenger: Floyd shortest circuit
Common locking table processing methods in Oracle