当前位置:网站首页>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 ? '空' : '非空'
}
边栏推荐
- 如何将 @Transactional 事务注解运用到炉火纯青?
- Leetcode skimming: binary tree 27 (delete nodes in the binary search tree)
- Analysis of DHCP dynamic host setting protocol
- 【从 0 开始学微服务】【00】课程概述
- 2022-07-07 Daily: Ian Goodfellow, the inventor of Gan, officially joined deepmind
- How to apply @transactional transaction annotation to perfection?
- 详解ThinkPHP支持的URL模式有四种普通模式、PATHINFO、REWRITE和兼容模式
- [learn wechat from 0] [00] Course Overview
- test
- The URL modes supported by ThinkPHP include four common modes, pathinfo, rewrite and compatibility modes
猜你喜欢
ACL 2022 | small sample ner of sequence annotation: dual tower Bert model integrating tag semantics
Leetcode skimming: binary tree 21 (verifying binary search tree)
ICLR 2022 | 基于对抗自注意力机制的预训练语言模型
人均瑞数系列,瑞数 4 代 JS 逆向分析
Awk of three swordsmen in text processing
leecode3. 无重复字符的最长子串
《ASP.NET Core 6框架揭秘》样章[200页/5章]
DHCP 动态主机设置协议 分析
TPG x AIDU|AI领军人才招募计划进行中!
Leetcode question brushing: binary tree 26 (insertion operation in binary search tree)
随机推荐
【Presto Profile系列】Timeline使用
MySQL导入SQL文件及常用命令
环境配置篇
“新红旗杯”桌面应用创意大赛2022
The URL modes supported by ThinkPHP include four common modes, pathinfo, rewrite and compatibility modes
红杉中国完成新一期90亿美元基金募集
日本政企员工喝醉丢失46万信息U盘,公开道歉又透露密码规则
Steps of building SSM framework
如何将 @Transactional 事务注解运用到炉火纯青?
What if the xshell evaluation period has expired
[crawler] avoid script detection when using selenium
ACL 2022 | 序列标注的小样本NER:融合标签语义的双塔BERT模型
Grep of three swordsmen in text processing
智云健康上市:市值150亿港元 SIG经纬与京新基金是股东
企业级自定义表单引擎解决方案(十二)--体验代码目录结构
基于NeRF的三维内容生成
详细介绍六种开源协议(程序员须知)
AUTOCAD——大于180度的角度标注、CAD直径符号怎么输入?
达晨与小米投的凌云光上市:市值153亿 为机器植入眼睛和大脑
Talk about four cluster schemes of redis cache, and their advantages and disadvantages