javascript - How to check whether a javascript object contains any values while having keys
问题描述
I have a javascript object which contains error messages:
err: {
subject: '',
body: '',
recipient: ''
},
i want to disable a submit button based on whether an error is present. in the above declaration, there are no errors present.
i'm aware of Object.values(err)
but i do not know how to use the resulting list, which has a length of 3.
how would I do this? thank you
解决方案
您可以使用Array.some()
来Object.values()
检查是否有一些错误消息
let obj = {
err: {
subject: '',
body: '',
recipient: ''
}
};
let someErrors = Object.values(obj.err).some(e => e.length);
console.log(someErrors);
推荐阅读
- javascript - 更改文本
在不影响其余部分的情况下
- omnet++ - “import org.car2x.veins.subprojects.veins_inet.VeinsInetManager”SimuLTE 的 Highway.ned 文件中的错误
- jquery - MySql 数据库 MVC 更改后刷新项目列表 - 页面上的实时更改
- mysql - 如何通过 POST 从选择数据表的选项中获取价值
- android - Trying to process a JSON array that is received through POST method (Retrofit2)
- javascript - 在猫鼬中添加到地图的异步问题
- windows - 在 Windows 中获取已连接网络域名的最佳方法
- android - 如果应用程序使用多个线程,它会比使用一个 UI 线程运行顺畅吗?
- python - 有没有办法隔离字符串的一部分?
- mysql - 通过主键从表中删除大量行键的最快方法?