当前位置:网站首页>盘点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)
objArray
Object.getOwnPropertyNames
Object.getOwnPropertyNames()
Object.getOwnPropertyNames()
Object.keys()
可枚举属性
enumerable
obj.name = "cxy"
enumerable
ture
false
for
Object.keys()
JSON.stringify()
Object.getOwnPropertyNames
Object.keys()
defineProperty
age
false
var 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()
false
toString
盘点判空方法
JSON.stringify
判空
JSON.stringify
let obj = {
name: "cxy"
}
console.log(JSON.stringify(obj) == '{}')
for in
判空
for in
false
ture
let forNull = (items) => {
for (let item in items) {
return false
}
return true
}
Object.getOwnPropertyNames
判空
Object.getOwnPropertyNames
length
let stuArray = Object.getOwnPropertyNames(obj)
console.log(stuArray.length === 0)
Object.keys()
判空
let stuArray = Object.getOwnPropertyNames(obj)
console.log(stuArray.length === 0)
hasOwnProperty
判空
hasOwnProperty
false
let forNull = (items) => {
for (let item in items) {
if(items.hasOwnProperty(item)){
return false
}
}
return true
}
边栏推荐
- SwiftUI 4 新功能之掌握 WeatherKit 和 Swift Charts
- 清华姚班程序员,网上征婚被骂?
- How to connect 5V serial port to 3.3V MCU serial port?
- In SQL, I want to set foreign keys. Why is this problem
- 百度数字人度晓晓在线回应网友喊话 应战上海高考英语作文
- 超标量处理器设计 姚永斌 第9章 指令执行 摘录
- 从工具升级为解决方案,有赞的新站位指向新价值
- 112.网络安全渗透测试—[权限提升篇10]—[Windows 2003 LPK.DDL劫持提权&msf本地提权]
- Superscalar processor design yaoyongbin Chapter 8 instruction emission excerpt
- When sink is consumed in mysql, the self incrementing primary key has been set in the database table. How to operate in Flink?
猜你喜欢
In SQL, I want to set foreign keys. Why is this problem
Fleet tutorial 19 introduction to verticaldivider separator component Foundation (tutorial includes source code)
Mise en œuvre du codage Huffman et du décodage avec interface graphique par MATLAB
Fleet tutorial 14 basic introduction to listtile (tutorial includes source code)
HCIA复习整理
Rationaldmis2022 array workpiece measurement
Sonar:cognitive complexity
【纹理特征提取】基于matlab局部二值模式LBP图像纹理特征提取【含Matlab源码 1931期】
人大金仓受邀参加《航天七〇六“我与航天电脑有约”全国合作伙伴大会》
Flet教程之 17 Card卡片组件 基础入门(教程含源码)
随机推荐
大佬们有没有人遇到过 flink oracle cdc,读取一个没有更新操作的表,隔十几秒就重复读取
MATLAB實現Huffman編碼譯碼含GUI界面
UP Meta—Web3.0世界创新型元宇宙金融协议
软件内部的定时炸弹:0-Day Log4Shell只是冰山一角
SwiftUI Swift 内功之 Swift 中使用不透明类型的 5 个技巧
Swiftui tutorial how to realize automatic scrolling function in 2 seconds
Basic introduction to the 16 tabs tab control in the fleet tutorial (the tutorial includes source code)
Cmu15445 (fall 2019) project 2 - hash table details
[system design] index monitoring and alarm system
zero-shot, one-shot和few-shot
Le Cluster kubernets en cours d'exécution veut ajuster l'adresse du segment réseau du pod
【最短路】ACwing 1127. 香甜的黄油(堆优化的dijsktra或spfa)
La voie du succès de la R & D des entreprises Internet à l’échelle des milliers de personnes
. Net Maui performance improvement
让数字管理好库存
Superscalar processor design yaoyongbin Chapter 8 instruction emission excerpt
【滤波跟踪】捷联惯导纯惯导解算matlab实现
Suggestions on one-stop development of testing life
[extraction des caractéristiques de texture] extraction des caractéristiques de texture de l'image LBP basée sur le mode binaire local de Matlab [y compris le code source de Matlab 1931]
【紋理特征提取】基於matlab局部二值模式LBP圖像紋理特征提取【含Matlab源碼 1931期】