当前位置:网站首页>判断一个对象是否是空对象的处理办法
判断一个对象是否是空对象的处理办法
2022-07-23 07:43:00 【孙叫兽】
目录
方法一,Object.keys()方法,返回对象的属性名组成的一个数组,若长度为0,则为空对象
前言
在维护客户基本信息的时候包含新客户及老客户,客户信息中有一组地址信息,这个地址数组包含经常居住地、工作地址及住所地址三种类型,经常居住地是必须有的,工作地址住所地址至少包含一个。这里上送的时候不能上传空对象,否则会报错。
phyaddress:[
jinchanginfo{
pytype:"H"
},
danweiinfo{
pytype:"F"
},
wordinfo{
pytype:"W"
}
]这个新客户或者老客户的住所地址或者工作地址有可能为空,这里只是简单举例,其他每种类型的地址可能很多,也有一些连续交易,这里只演示最简单的情况,把过滤的值每种类型只有一个满足条件即可
方法一,Object.keys()方法,返回对象的属性名组成的一个数组,若长度为0,则为空对象
let danweiinfo= {};
Object.keys(danweiinfo).length === 0 && empty.constructor === Object;let wordinfo= {};
Object.keys(wordinfo).length === 0 && empty.constructor === Object;方法二、for in循环
let result=function(obj){
for(let key in obj){
return false;//若不为空,可遍历,返回false
}
return true;
}
console.log(result(obj));//返回true方法三:将对象转换成字符串,再判断是否等于“{}”
let obj={};
console.log(JSON.stringify(obj)==="{}");
//返回true基本就是这种,然后我们把新数据组装成新的地址数组再上送,如果同一种类型的地址很多,我们只取客户最新修改的那一条记录,可以使用filte方法过滤出同种类型的地址对象,然后使用pop()方法取最新的一条地址对象,然后判断如果有值就push到新数组再组装赋值。
边栏推荐
- Is it risky and safe to open an account?
- Redis常用命令
- Light chain dissection / tree chain dissection
- 内存取证之NSSCTF-OtterCTF 2018(复现赛)
- Google Earth engine -- a small bug in gee. Images of transcontinental boundaries cannot be obtained
- KingbaseES 格式化函数
- Image processing 5: expansion
- [laser principle and application -7]: semiconductor refrigeration sheet and Tec thermostat
- Interviewer: have you learned about the underlying implementation of reentrantlock? tell us your opinion
- LeetCode_2341_数组能形成多少数对
猜你喜欢
随机推荐
数千个数据库、遍布全国的物理机,京东物流全量上云实录 | 卓越技术团队访谈录
Image processing 2: mean filtering
解决报错:Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location: “
单例模式实现及防止反射与序列化
Unity制作简单拦截近防炮——如何预测打击目标
LeetCode_ 46_ Full Permutation
LeetCode_491_递增子序列
BERT 文章翻译
解决MySQL向表中增加数据插入中文乱码问题
图像处理5:膨胀
Talking about the CPU type of anroid device and the placement directory of so files
常用的鼠标事件和键盘事件
Kingbaseesv8r6 difference of xmin under different isolation levels
Detailed understanding of GRE and mGRE; OSPF basic configuration knowledge
去除标题栏
Unity makes simple intercepting close range artillery - how to predict the strike target
Image processing 6: top level file
[激光器原理与应用-7]: 半导体制冷片与TEC温控器
Kotlin - 挂起函数 suspend
数据库系统原理与应用教程(045)—— MySQL 查询(七):聚合函数








