当前位置:网站首页>盘点JS判断空对象的几大方法
盘点JS判断空对象的几大方法
2022-07-07 10:07:00 【InfoQ】
知识准备
Object.keys
Object.keys() var obj = {
name: "cxy",
age: "19"
};
var objArray = Object.getOwnPropertyNames(obj);
console.log(objArray)

objArrayObject.getOwnPropertyNames
Object.getOwnPropertyNames()Object.getOwnPropertyNames()Object.keys()可枚举属性
enumerableobj.name = "cxy"enumerableturefalseforObject.keys()JSON.stringify()Object.getOwnPropertyNamesObject.keys()definePropertyagefalsevar stuObj = {
name: "cxy"
}
Object.defineProperty(stuObj, 'age', {
value: "18",
enumerable: false
});
console.log(Object.keys(stuObj))
console.log(Object.getOwnPropertyNames(stuObj))

hasOwnProperty
hasOwnProperty() var stuObj = {
name: "cxy"
}
console.log(stuObj.hasOwnProperty('name'))

hasOwnProperty()falsetoString盘点判空方法
JSON.stringify
判空
JSON.stringifylet obj = {
name: "cxy"
}
console.log(JSON.stringify(obj) == '{}')
for in
判空
for infalseturelet forNull = (items) => {
for (let item in items) {
return false
}
return true
}
Object.getOwnPropertyNames
判空
Object.getOwnPropertyNameslengthlet stuArray = Object.getOwnPropertyNames(obj)
console.log(stuArray.length === 0)
Object.keys()
判空
let stuArray = Object.getOwnPropertyNames(obj)
console.log(stuArray.length === 0)
hasOwnProperty
判空
hasOwnPropertyfalselet forNull = (items) => {
for (let item in items) {
if(items.hasOwnProperty(item)){
return false
}
}
return true
}
边栏推荐
- Time bomb inside the software: 0-day log4shell is just the tip of the iceberg
- Rationaldmis2022 array workpiece measurement
- Swiftui tutorial how to realize automatic scrolling function in 2 seconds
- 软件内部的定时炸弹:0-Day Log4Shell只是冰山一角
- NPC Jincang was invited to participate in the "aerospace 706" I have an appointment with aerospace computer "national Partner Conference
- Zero shot, one shot and few shot
- Flet教程之 17 Card卡片组件 基础入门(教程含源码)
- Flet教程之 15 GridView 基础入门(教程含源码)
- Unity 贴图自动匹配材质工具 贴图自动添加到材质球工具 材质球匹配贴图工具 Substance Painter制作的贴图自动匹配材质球工具
- 【滤波跟踪】基于matlab捷联惯导仿真【含Matlab源码 1935期】
猜你喜欢

Summed up 200 Classic machine learning interview questions (with reference answers)

SwiftUI Swift 内功之如何在 Swift 中进行自动三角函数计算

@Bean与@Component用在同一个类上,会怎么样?

【滤波跟踪】捷联惯导纯惯导解算matlab实现

Le Cluster kubernets en cours d'exécution veut ajuster l'adresse du segment réseau du pod

《通信软件开发与应用》课程结业报告
![[filter tracking] comparison between EKF and UKF based on MATLAB extended Kalman filter [including Matlab source code 1933]](/img/90/ef2400754cbf3771535196f6822992.jpg)
[filter tracking] comparison between EKF and UKF based on MATLAB extended Kalman filter [including Matlab source code 1933]

Talk about SOC startup (VI) uboot startup process II

【纹理特征提取】基于matlab局部二值模式LBP图像纹理特征提取【含Matlab源码 1931期】

超标量处理器设计 姚永斌 第9章 指令执行 摘录
随机推荐
Swiftui swift internal skill how to perform automatic trigonometric function calculation in swift
CMU15445 (Fall 2019) 之 Project#2 - Hash Table 详解
大佬们有没有人遇到过 flink oracle cdc,读取一个没有更新操作的表,隔十几秒就重复读取
如何理解服装产业链及供应链
C#中在路径前加@的作用
软件内部的定时炸弹:0-Day Log4Shell只是冰山一角
[filter tracking] strapdown inertial navigation simulation based on MATLAB [including Matlab source code 1935]
110. Network security penetration test - [privilege promotion 8] - [windows sqlserver xp_cmdshell stored procedure authorization]
R language uses the quantile function to calculate the quantile of the score value (20%, 40%, 60%, 80%), uses the logical operator to encode the corresponding quantile interval (quantile) into the cla
本地navicat连接liunx下的oracle报权限不足
Cmu15445 (fall 2019) project 2 - hash table details
Onedns helps college industry network security
Talk about SOC startup (VI) uboot startup process II
什么是局域网域名?如何解析?
MySQL安装常见报错处理大全
清华姚班程序员,网上征婚被骂?
Talk about SOC startup (x) kernel startup pilot knowledge
Fleet tutorial 19 introduction to verticaldivider separator component Foundation (tutorial includes source code)
UP Meta—Web3.0世界创新型元宇宙金融协议
[system design] index monitoring and alarm system