当前位置:网站首页>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 ? '空' : '非空'
}
边栏推荐
- The URL modes supported by ThinkPHP include four common modes, pathinfo, rewrite and compatibility modes
- 博文推荐|Apache Pulsar 跨地域复制方案选型实践
- Smart cloud health listed: with a market value of HK $15billion, SIG Jingwei and Jingxin fund are shareholders
- Sample chapter of "uncover the secrets of asp.net core 6 framework" [200 pages /5 chapters]
- Sequoia China completed the new phase of $9billion fund raising
- Users, groups, and permissions
- - Oui. Migration entièrement automatisée de la Sous - base de données des tableaux d'effets sous net
- 2022-07-07 Daily: Ian Goodfellow, the inventor of Gan, officially joined deepmind
- Awk of three swordsmen in text processing
- HZOJ #236. Recursive implementation of combinatorial enumeration
猜你喜欢
Master formula. (used to calculate the time complexity of recursion.)
为租客提供帮助
人均瑞数系列,瑞数 4 代 JS 逆向分析
Star Enterprise Purdue technology layoffs: Tencent Sequoia was a shareholder who raised more than 1billion
《开源圆桌派》第十一期“冰与火之歌”——如何平衡开源与安全间的天然矛盾?
滑轨步进电机调试(全国海洋航行器大赛)(STM32主控)
NPM instal reports agent or network problems
Importance of database security
Leetcode skimming: binary tree 27 (delete nodes in the binary search tree)
Practical example of propeller easydl: automatic scratch recognition of industrial parts
随机推荐
HZOJ #235. Recursive implementation of exponential enumeration
[learn microservices from 0] [03] explore the microservice architecture
HZOJ #236. 递归实现组合型枚举
Go语言学习笔记-结构体(Struct)
SSM框架搭建的步骤
[learn wechat from 0] [00] Course Overview
HZOJ #236. Recursive implementation of combinatorial enumeration
基于NeRF的三维内容生成
Leetcode brush question: binary tree 24 (the nearest common ancestor of binary tree)
Leetcode skimming: binary tree 23 (mode in binary search tree)
Coscon'22 community convening order is coming! Open the world, invite all communities to embrace open source and open a new world~
Day-20 file operation, recursive copy, serialization
layer弹出层的关闭问题
Cmu15445 (fall 2019) project 2 - hash table details
Shortcut key of Bash
[learn micro services from 0] [02] move from single application to service
【从 0 开始学微服务】【00】课程概述
Four functions of opencv
Guangzhou held work safety conference
【无标题】