根据数组中的值验证对象是否为空 - Javascript
Validate if object is empty from values in an array - Javascript
我有如下企业的数据:
{
"business": {
"type": [
"LLC",
"Corporation"
],
"LLC": {
"status": "active",
"profits": 1000000,
"period": "yearly"
},
"corporation": {},
"partnership": {}
}
}
如何验证“corporation”对象,以便如果“type”数组包含字符串“Corporation”,“corporation”对象不能为空?
我试过使用 validate.js 检查它是否为空,它有效,但我不能专门将字符串放入类型数组中以验证它。
const validate = require('validate.js');
if (validate.isEmpty(business.LLC)) {
return wrapper.error('fail', 'Object is empty');
}
非常感谢您的帮助
let object = {
"business": {
"type": [
"LLC",
"Corporation"
],
"LLC": {
"status": "active",
"profits": 1000000,
"period": "yearly"
},
"corporation": {},
"partnership": {}
}
}
使用includes检查数组中的元素,
object.business.type.includes("Corporation")
我想通了,也没有意识到,谢谢dinesh的回答!
所以我需要做的是先检查数组,再检查下面的对象。
if (payload.business.type.includes('Corporation')) {
if (validate.isEmpty(payload.business.corporation)) {
return wrapper.error('fail', 'Object is empty');
}
}
我有如下企业的数据:
{
"business": {
"type": [
"LLC",
"Corporation"
],
"LLC": {
"status": "active",
"profits": 1000000,
"period": "yearly"
},
"corporation": {},
"partnership": {}
}
}
如何验证“corporation”对象,以便如果“type”数组包含字符串“Corporation”,“corporation”对象不能为空?
我试过使用 validate.js 检查它是否为空,它有效,但我不能专门将字符串放入类型数组中以验证它。
const validate = require('validate.js');
if (validate.isEmpty(business.LLC)) {
return wrapper.error('fail', 'Object is empty');
}
非常感谢您的帮助
let object = {
"business": {
"type": [
"LLC",
"Corporation"
],
"LLC": {
"status": "active",
"profits": 1000000,
"period": "yearly"
},
"corporation": {},
"partnership": {}
}
}
使用includes检查数组中的元素,
object.business.type.includes("Corporation")
我想通了,也没有意识到,谢谢dinesh的回答!
所以我需要做的是先检查数组,再检查下面的对象。
if (payload.business.type.includes('Corporation')) {
if (validate.isEmpty(payload.business.corporation)) {
return wrapper.error('fail', 'Object is empty');
}
}