V
V
vaskadogana2017-09-01 11:15:23
JSON
vaskadogana, 2017-09-01 11:15:23

What is the correct way to check JSON for nested key?

For example, there is json

var a={
           b:{
               c:5
              },
           c: null
}

if ac is accessed, it will return null
, but if acc , then the error is
Uncaught TypeError: Cannot read property 'c' of undefined
at: 1: 4
I want to write a function to avoid this error.
Like this
function ifEmty(data){
  if (data == null || data == undefined){
    return undefined
  }else {
    return data
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Kornilov, 2017-09-01
@vaskadogana

a.c && a.c.c
It won't throw an error anymore.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question