I have a JSON object I’m retrieving from a server, sometimes its really simple such as:
{ "urls": ["http://google.com", "http://bing.com"] }
However I need to check if urls['tasks']['ipv6'] contains true, the code I’m using is:
if (urls['tasks']['ipv6'] === true) {
console.log('true');
}
But because urls[‘tasks’] doesn’t exist I get the following error:
Uncaught TypeError: Cannot read property 'ipv6' of undefined
Is there an easy way to do this? I’m having a really hard time find a solution online. I’ve also tried jQuery’s $.isEmptyObject and would be happy with a jQuery based solution. For reference I’m actually using this code in node.js.
This should do the trick for you.
You can add
urlschecking at left as well if you need to and you can omit the middle checkurls["tasks"]["ipv6"], because if it’sundefinedit definitely isn’ttrue.The thing is that you have to check first whether upper level properties exist before drilling down. And since Javascript (as well as majority of today’s languages) doesn’t use full boolean evolution this works.