当前位置:网站首页>盘点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
}
边栏推荐
- 30. Few-shot Named Entity Recognition with Self-describing Networks 阅读笔记
- Have you ever met flick Oracle CDC, read a table without update operation, and read it repeatedly every ten seconds
- Review and arrangement of HCIA
- Test the foundation of development, and teach you to prepare for a fully functional web platform environment
- Enclosed please find. Net Maui's latest learning resources
- MATLAB實現Huffman編碼譯碼含GUI界面
- 111.网络安全渗透测试—[权限提升篇9]—[Windows 2008 R2内核溢出提权]
- Suggestions on one-stop development of testing life
- SwiftUI 4 新功能之掌握 WeatherKit 和 Swift Charts
- Improve application security through nonce field of play integrity API
猜你喜欢

In SQL, I want to set foreign keys. Why is this problem

Mise en œuvre du codage Huffman et du décodage avec interface graphique par MATLAB

Talk about SOC startup (x) kernel startup pilot knowledge

How to connect 5V serial port to 3.3V MCU serial port?

小红书微服务框架及治理等云原生业务架构演进案例

CMU15445 (Fall 2019) 之 Project#2 - Hash Table 详解

【滤波跟踪】捷联惯导纯惯导解算matlab实现

Poor math students who once dropped out of school won the fields award this year

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

Rationaldmis2022 array workpiece measurement
随机推荐
【紋理特征提取】基於matlab局部二值模式LBP圖像紋理特征提取【含Matlab源碼 1931期】
R language Visual facet chart, hypothesis test, multivariable grouping t-test, visual multivariable grouping faceting boxplot, and add significance levels and jitter points
[neural network] convolutional neural network CNN [including Matlab source code 1932]
SwiftUI Swift 内功之 Swift 中使用不透明类型的 5 个技巧
软件内部的定时炸弹:0-Day Log4Shell只是冰山一角
Present pod information to the container through environment variables
竟然有一半的人不知道 for 与 foreach 的区别???
MATLAB實現Huffman編碼譯碼含GUI界面
Mastering the new functions of swiftui 4 weatherkit and swift charts
Test the foundation of development, and teach you to prepare for a fully functional web platform environment
How to write test cases for test coupons?
Unity中SmoothStep介绍和应用: 溶解特效优化
La voie du succès de la R & D des entreprises Internet à l’échelle des milliers de personnes
Review and arrangement of HCIA
Complete collection of common error handling in MySQL installation
Let digital manage inventory
sink 消费 到 MySQL, 数据库表里面已经设置了 自增主键, flink 里面,如何 操作?
千人規模互聯網公司研發效能成功之路
The running kubernetes cluster wants to adjust the network segment address of pod
How to connect 5V serial port to 3.3V MCU serial port?