当前位置:网站首页>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 '
}
边栏推荐
猜你喜欢
[untitled]
Per capita Swiss number series, Swiss number 4 generation JS reverse analysis
红杉中国完成新一期90亿美元基金募集
.Net下极限生产力之efcore分表分库全自动化迁移CodeFirst
滑轨步进电机调试(全国海洋航行器大赛)(STM32主控)
Awk of three swordsmen in text processing
飞桨EasyDL实操范例:工业零件划痕自动识别
10 张图打开 CPU 缓存一致性的大门
AUTOCAD——大于180度的角度标注、CAD直径符号怎么输入?
leecode3. 无重复字符的最长子串
随机推荐
. Net ultimate productivity of efcore sub table sub database fully automated migration codefirst
飞桨EasyDL实操范例:工业零件划痕自动识别
将数学公式在el-table里面展示出来
DrawerLayout禁止侧滑显示
Query whether a field has an index with MySQL
Four functions of opencv
日本政企员工喝醉丢失46万信息U盘,公开道歉又透露密码规则
【Presto Profile系列】Timeline使用
Sed of three swordsmen in text processing
聊聊伪共享
[learn wechat from 0] [00] Course Overview
如何让electorn打开的新窗口在window任务栏上面
为租客提供帮助
Image pixel read / write operation
Sequoia China completed the new phase of $9billion fund raising
Find ID value MySQL in string
[untitled]
线程池拒绝策略最佳实践
Cloud detection 2020: self attention generation countermeasure network for cloud detection in high-resolution remote sensing images
error LNK2019: 无法解析的外部符号