当前位置:网站首页>盘点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
}
边栏推荐
- Complete collection of common error handling in MySQL installation
- VIM command mode and input mode switching
- Swiftui swift internal skill: five skills of using opaque type in swift
- When sink is consumed in mysql, the self incrementing primary key has been set in the database table. How to operate in Flink?
- Stm32f1 and stm32subeide programming example -max7219 drives 8-bit 7-segment nixie tube (based on SPI)
- Flet教程之 16 Tabs 选项卡控件 基础入门(教程含源码)
- Matlab implementation of Huffman coding and decoding with GUI interface
- Talk about SOC startup (IX) adding a new board to uboot
- zero-shot, one-shot和few-shot
- Unity中SmoothStep介绍和应用: 溶解特效优化
猜你喜欢
zero-shot, one-shot和few-shot
Summed up 200 Classic machine learning interview questions (with reference answers)
UP Meta—Web3.0世界创新型元宇宙金融协议
Talk about SOC startup (VII) uboot startup process III
超标量处理器设计 姚永斌 第8章 指令发射 摘录
NPC Jincang was invited to participate in the "aerospace 706" I have an appointment with aerospace computer "national Partner Conference
MATLAB實現Huffman編碼譯碼含GUI界面
人大金仓受邀参加《航天七〇六“我与航天电脑有约”全国合作伙伴大会》
Test the foundation of development, and teach you to prepare for a fully functional web platform environment
Le Cluster kubernets en cours d'exécution veut ajuster l'adresse du segment réseau du pod
随机推荐
What are the top-level domain names? How is it classified?
Fleet tutorial 15 introduction to GridView Basics (tutorial includes source code)
Steps of redis installation and self startup configuration under CentOS system
【紋理特征提取】基於matlab局部二值模式LBP圖像紋理特征提取【含Matlab源碼 1931期】
【数据聚类】基于多元宇宙优化DBSCAN实现数据聚类分析附matlab代码
Flet教程之 18 Divider 分隔符组件 基础入门(教程含源码)
What development models did you know during the interview? Just read this one
Unity中SmoothStep介绍和应用: 溶解特效优化
<No. 8> 1816. Truncate sentences (simple)
Internet Protocol
【最短路】ACwing 1127. 香甜的黄油(堆优化的dijsktra或spfa)
防红域名生成的3种方法介绍
How to write test cases for test coupons?
How to understand the clothing industry chain and supply chain
[filter tracking] strapdown inertial navigation simulation based on MATLAB [including Matlab source code 1935]
STM32F1与STM32CubeIDE编程实例-315M超再生无线遥控模块驱动
Flet tutorial 17 basic introduction to card components (tutorial includes source code)
超标量处理器设计 姚永斌 第8章 指令发射 摘录
Zero shot, one shot and few shot
从工具升级为解决方案,有赞的新站位指向新价值