如何正确地将propertyName传递给isValid()函数,以便能够检查它是否不为空?当我直接检查“ searchInside.attendeeList”时,它起作用了!
function isValid(searchInside, propertyName) {
if(searchInside.propertyName)
console.log("this doesnt work");
if(searchInside.attendeeList)
console.log("this works");
}
var requestBody = {
"meetingType": "Company",
"emailSendingReason": "CREATED",
"attendeeList": [
{
"employeeId": "12345",
"employeeDisplayName": "abc, xyz",
"callInFlag": false,
"infoPackRequiredFlag": true,
"inviteForInfoOnly": true
},
{
"employeeId": "374684678",
"employeeDisplayName": "xyz, poi",
"callInFlag": true,
"infoPackRequiredFlag": true,
"inviteForInfoOnly": false
}
],
"thirdPartyAttendee": {}
};
isValid(requestBody, 'attendeeList');
答案 0 :(得分:0)
尝试searchInside.hasOwnProperty(propertyName)
。对象 hasOwnProperty()检查对象密钥并返回布尔结果。
function isValid(searchInside, propertyName) {
if(searchInside.hasOwnProperty( propertyName)){
console.log("this works");
} else console.log("this doesn't work");
}
var requestBody = {
"meetingType": "Company",
"emailSendingReason": "CREATED",
"attendeeList": [
{
"employeeId": "12345",
"employeeDisplayName": "abc, xyz",
"callInFlag": false,
"infoPackRequiredFlag": true,
"inviteForInfoOnly": true
},
{
"employeeId": "374684678",
"employeeDisplayName": "xyz, poi",
"callInFlag": true,
"infoPackRequiredFlag": true,
"inviteForInfoOnly": false
}
],
"thirdPartyAttendee": {}
};
isValid(requestBody, 'attendeeList');
答案 1 :(得分:0)
您已经正确将它作为字符串传递了。 在您的 isValid()函数中,您可以像这样引用它:
function isValid(searchInside, propertyName) {
if(searchInside[propertyName]){
console.log("this works");
}
}
答案 2 :(得分:0)
function isValid(searchInside, propertyName) {
if(typeof searchInside[propertyName] !== 'undefined')
console.log("this doesnt work");
if(searchInside.attendeeList)
console.log("this works");
}