当前位置:网站首页>JS判断一个对象是否为空
JS判断一个对象是否为空
2022-07-07 11:01:00 【Dax1_】
1. for…in
利用for...in
遍历对象,如果对象存在属性则返回“非空”,否则返回“空”
function fn(obj) {
for (let key in obj) {
return '非空'
}
return '空'
}
2. JSON.Stringify()
利用JSON.stringify()
将对象序列化,若序列化后的结果为{}
,则对象是空对象
function fn2(obj) {
let res = JSON.stringify(obj)
return res === '{}' ? '空' : '非空'
}
3. Object.keys()
利用Object.keys()
返回一个由给定对象的自身可枚举属性组成的数组,若返回一个空数组,则给定对象为空对象。
function fn3(obj) {
return Object.keys(obj).length === 0 ? '空' : '非空'
}
边栏推荐
- 【无标题】
- 关于 appium 如何关闭 app (已解决)
- 共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
- Importance of database security
- Awk of three swordsmen in text processing
- Talk about four cluster schemes of redis cache, and their advantages and disadvantages
- Cookie
- Polymorphism, final, etc
- @What is the difference between resource and @autowired?
- MySQL importing SQL files and common commands
猜你喜欢
- Oui. Migration entièrement automatisée de la Sous - base de données des tableaux d'effets sous net
Coscon'22 community convening order is coming! Open the world, invite all communities to embrace open source and open a new world~
【学习笔记】AGC010
Practical example of propeller easydl: automatic scratch recognition of industrial parts
【无标题】
关于 appium 如何关闭 app (已解决)
“新红旗杯”桌面应用创意大赛2022
Lingyunguang of Dachen and Xiaomi investment is listed: the market value is 15.3 billion, and the machine is implanted into the eyes and brain
COSCon'22 社区召集令来啦!Open the World,邀请所有社区一起拥抱开源,打开新世界~
Per capita Swiss number series, Swiss number 4 generation JS reverse analysis
随机推荐
Talk about four cluster schemes of redis cache, and their advantages and disadvantages
HZOJ #236. Recursive implementation of combinatorial enumeration
test
Shortcut key of Bash
What if the xshell evaluation period has expired
Aosikang biological sprint scientific innovation board of Hillhouse Investment: annual revenue of 450million yuan, lost cooperation with kangxinuo
Users, groups, and permissions
Conversion from non partitioned table to partitioned table and precautions
.Net下極限生產力之efcore分錶分庫全自動化遷移CodeFirst
[difficult and miscellaneous]pip running suddenly appears modulenotfounderror: no module named 'pip‘
Leetcode brush questions: binary tree 19 (merge binary tree)
[untitled]
HZOJ #240. Graphic printing IV
HZOJ #236. 递归实现组合型枚举
Design and implementation of communication protocol
. Net ultimate productivity of efcore sub table sub database fully automated migration codefirst
免费手机号码归属地API查询接口
认养一头牛冲刺A股:拟募资18.5亿 徐晓波持股近40%
What are the benefits of ip2long?
leecode3. 无重复字符的最长子串