首页 > 解决方案 > 动态检查 json 属性

问题描述

如何将 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');

标签: javascript

解决方案


function isValid(searchInside, propertyName) {
  if(typeof searchInside[propertyName] !== 'undefined')
    console.log("this doesnt work");
  if(searchInside.attendeeList)
    console.log("this works");
}

推荐阅读