admin 管理员组文章数量: 1086019
I have an object:
const obj = {
name: "",
age: null
}
How can I check if all attributes is null or "" with lodash ? Is so, return true, if some attributes is not null or "" then false.
I have an object:
const obj = {
name: "",
age: null
}
How can I check if all attributes is null or "" with lodash ? Is so, return true, if some attributes is not null or "" then false.
Share Improve this question edited Mar 27, 2020 at 10:58 Ori Drori 194k32 gold badges238 silver badges229 bronze badges asked Mar 27, 2020 at 10:55 user13089222user130892222 Answers
Reset to default 6With lodash you can use _.every()
to check if all properties are empty:
const obj = {
name: '',
age: null
}
const result = _.every(obj, v => v === '' || _.isNull(v))
console.log(result)
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.17.15/lodash.js"></script>
You could use Object.values
, Array.every
and Array.includes
:
const obj1 = { name: "", age: null }
const obj2 = { name: "", age: null, b: 0 }
const obj3 = { name: "", age: null, b: NaN }
const obj4 = { name: "", age: null, b: false }
const obj5 = { name: "", age: null, b: undefined }
function checkEmptyOrNull(obj) {
return Object.values(obj).every(v => ['', null].includes(v))
}
console.log(checkEmptyOrNull(obj1))
console.log(checkEmptyOrNull(obj2))
console.log(checkEmptyOrNull(obj3))
console.log(checkEmptyOrNull(obj4))
console.log(checkEmptyOrNull(obj5))
本文标签: javascriptHow to check if all attributes is null or empty string with lodashStack Overflow
版权声明:本文标题:javascript - How to check if all attributes is null or empty string with lodash? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744057840a2526171.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论