当前位置:网站首页>JS determines whether an object is empty
JS determines whether an object is empty
2022-07-07 13:08:00 【Dax1_】
1. for…in
utilize for...in Traversing objects , If the object has properties, return “ Non empty ”, Otherwise return to “ empty ”
function fn(obj) {
for (let key in obj) {
return ' Non empty '
}
return ' empty '
}
2. JSON.Stringify()
utilize JSON.stringify() Serialize objects , If the result after serialization is {}, Then the object is empty
function fn2(obj) {
let res = JSON.stringify(obj)
return res === '{}' ? ' empty ' : ' Non empty '
}
3. Object.keys()
utilize Object.keys() Returns the self of a given object Enumerable properties Array of components , If you return an empty array , Then the given object is an empty object .
function fn3(obj) {
return Object.keys(obj).length === 0 ? ' empty ' : ' Non empty '
}
边栏推荐
猜你喜欢

飞桨EasyDL实操范例:工业零件划痕自动识别

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

Image pixel read / write operation

高瓴投的澳斯康生物冲刺科创板:年营收4.5亿 丢掉与康希诺合作

MATLAB中polarscatter函数使用

How to continue after handling chain interruption / sub chain error removed from scheduling

About how appium closes apps (resolved)

AUTOCAD——大于180度的角度标注、CAD直径符号怎么输入?

人均瑞数系列,瑞数 4 代 JS 逆向分析

智云健康上市:市值150亿港元 SIG经纬与京新基金是股东
随机推荐
Leetcode skimming: binary tree 21 (verifying binary search tree)
About how appium closes apps (resolved)
Session
What kind of methods or functions can you view the laravel version of a project?
10 张图打开 CPU 缓存一致性的大门
“新红旗杯”桌面应用创意大赛2022
Per capita Swiss number series, Swiss number 4 generation JS reverse analysis
[binary tree] delete points to form a forest
MySQL importing SQL files and common commands
Leetcode skimming: binary tree 23 (mode in binary search tree)
- Oui. Migration entièrement automatisée de la Sous - base de données des tableaux d'effets sous net
@What is the difference between resource and @autowired?
COSCon'22 社区召集令来啦!Open the World,邀请所有社区一起拥抱开源,打开新世界~
Go语言学习笔记-结构体(Struct)
初学XML
.Net下极限生产力之efcore分表分库全自动化迁移CodeFirst
在字符串中查找id值MySQL
Sequoia China completed the new phase of $9billion fund raising
【无标题】
《ASP.NET Core 6框架揭秘》样章[200页/5章]