0

I have the following array generated by PHP:

{
    "101265" : {
        "1" : {
            "1" : 165,
            "2" : 120
        },
        "2" : {
            "10" : 5
        }
    },
    "101382" : {
        "1" : {
            "1" : 810,
            "2" : 120
        }
    },
    "101388" : {
        "1" : {
            "13" : 240
        }
    },
    "101449" : {
        "70" : {
            "127" : 30
        }
    }
}

In order for me to avoid undefined variable error when trying to access totalTimePerRate[ticketId][serviceId][serviceRateId]

I have to use the following checks:

var totalTime = (typeof totalTimePerRate[ticketId] !== 'undefined') ? (typeof totalTimePerRate[ticketId][serviceId] !== 'undefined') ? (typeof totalTimePerRate[ticketId][serviceId][serviceRateId] !== 'undefined') ? totalTimePerRate[ticketId][serviceId][serviceRateId] : false : false : false;

Otherwise it will throw an error if such ticketId, serviceId or serviceRateId not found.

Could you please advise on how to minimize the code of such checks?

Thanks.

Alex G
  • 3,048
  • 10
  • 39
  • 78

0 Answers0