当前位置:网站首页>盘点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
}
边栏推荐
- 【神经网络】卷积神经网络CNN【含Matlab源码 1932期】
- 相机标定(2): 单目相机标定总结
- <No. 9> 1805. 字符串中不同整数的数目 (简单)
- Talk about SOC startup (VII) uboot startup process III
- What is high cohesion and low coupling?
- Ask about the version of flinkcdc2.2.0, which supports concurrency. Does this concurrency mean Multiple Parallelism? Now I find that mysqlcdc is full
- Flet教程之 16 Tabs 选项卡控件 基础入门(教程含源码)
- Tsinghua Yaoban programmers, online marriage was scolded?
- Mise en œuvre du codage Huffman et du décodage avec interface graphique par MATLAB
- Summed up 200 Classic machine learning interview questions (with reference answers)
猜你喜欢

Nuclear boat (I): when "male mothers" come into reality, can the biotechnology revolution liberate women?

Suggestions on one-stop development of testing life

<No. 8> 1816. 截断句子 (简单)

【最短路】Acwing1128信使:floyd最短路

The running kubernetes cluster wants to adjust the network segment address of pod
![[texture feature extraction] LBP image texture feature extraction based on MATLAB local binary mode [including Matlab source code 1931]](/img/65/bf1d0f82878a49041e8c2b3a84bc15.png)
[texture feature extraction] LBP image texture feature extraction based on MATLAB local binary mode [including Matlab source code 1931]

Superscalar processor design yaoyongbin Chapter 9 instruction execution excerpt

2022 年第八届“认证杯”中国高校风险管理与控制能力挑战赛
![[filter tracking] strapdown inertial navigation pure inertial navigation solution matlab implementation](/img/14/6e440f3c4e04d9b322f0c3f43e213c.png)
[filter tracking] strapdown inertial navigation pure inertial navigation solution matlab implementation
![[data clustering] realize data clustering analysis based on multiverse optimization DBSCAN with matlab code](/img/83/0652e3138b87a4741dd8261a24d1e8.png)
[data clustering] realize data clustering analysis based on multiverse optimization DBSCAN with matlab code
随机推荐
Detailed explanation of debezium architecture of debezium synchronization
STM32F1与STM32CubeIDE编程实例-MAX7219驱动8位7段数码管(基于SPI)
Zero shot, one shot and few shot
In SQL, I want to set foreign keys. Why is this problem
软件内部的定时炸弹:0-Day Log4Shell只是冰山一角
超标量处理器设计 姚永斌 第9章 指令执行 摘录
Fleet tutorial 14 basic introduction to listtile (tutorial includes source code)
[shortest circuit] acwing 1127 Sweet butter (heap optimized dijsktra or SPFA)
<No. 8> 1816. 截断句子 (简单)
Present pod information to the container through environment variables
In my limited software testing experience, a full-time summary of automation testing experience
Completion report of communication software development and Application
Sonar:cognitive complexity
110.网络安全渗透测试—[权限提升篇8]—[Windows SqlServer xp_cmdshell存储过程提权]
What are the technical differences in source code anti disclosure
<No. 8> 1816. Truncate sentences (simple)
Rationaldmis2022 array workpiece measurement
Neural approvals to conversational AI (1)
【滤波跟踪】基于matlab扩展卡尔曼滤波EKF和无迹卡尔曼滤波UKF比较【含Matlab源码 1933期】
powershell cs-UTF-16LE编码上线