当前位置:网站首页>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 ? '空' : '非空'
}
边栏推荐
- Differences between MySQL storage engine MyISAM and InnoDB
- . Net ultimate productivity of efcore sub table sub database fully automated migration codefirst
- Steps of building SSM framework
- . Net ultimate productivity of efcore sub table sub database fully automated migration codefirst
- Sample chapter of "uncover the secrets of asp.net core 6 framework" [200 pages /5 chapters]
- HZOJ #236. 递归实现组合型枚举
- Unity 构建错误:当前上下文中不存在名称“EditorUtility”
- Cookie
- Leetcode skimming: binary tree 21 (verifying binary search tree)
- Query whether a field has an index with MySQL
猜你喜欢

明星企业普渡科技大裁员:曾募资超10亿 腾讯红杉是股东

Per capita Swiss number series, Swiss number 4 generation JS reverse analysis

【无标题】

智云健康上市:市值150亿港元 SIG经纬与京新基金是股东

滑轨步进电机调试(全国海洋航行器大赛)(STM32主控)

“新红旗杯”桌面应用创意大赛2022

Blog recommendation | Apache pulsar cross regional replication scheme selection practice

Go language learning notes - structure

ACL 2022 | 序列标注的小样本NER:融合标签语义的双塔BERT模型

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
随机推荐
Star Enterprise Purdue technology layoffs: Tencent Sequoia was a shareholder who raised more than 1billion
Day-16 set
[crawler] avoid script detection when using selenium
Conversion from non partitioned table to partitioned table and precautions
What kind of methods or functions can you view the laravel version of a project?
【无标题】
PHP调用纯真IP数据库返回具体地址
Ip2long and long2ip analysis
【Presto Profile系列】Timeline使用
Cmu15445 (fall 2019) project 2 - hash table details
谷歌浏览器如何重置?谷歌浏览器恢复默认设置?
ip2long之后有什么好处?
Layer pop-up layer closing problem
Per capita Swiss number series, Swiss number 4 generation JS reverse analysis
Steps of building SSM framework
通过Keil如何查看MCU的RAM与ROM使用情况
在字符串中查找id值MySQL
日本政企员工喝醉丢失46万信息U盘,公开道歉又透露密码规则
File operation command
How to continue after handling chain interruption / sub chain error removed from scheduling