-1

I get a data from backend, the data is JSON format and nested,like

[{
  a: {
    b: {
      c: 'value',
      ...
    },
    ...
  }
},....]

or maybe

[{
  a: {
    b: {
      // no  key 'c'
      ...
    },
    ...
  }
},....]

may also be

[{
  a: {
    // no key 'b'
    ...
  }
},....]

I have to get 'a.b.c' safely, is there any good way to do it?

'a.b.c.d'?

NieWei
  • 171
  • 1
  • 1
  • 5

1 Answers1

0

You can use hasOwnProperty() like this

if(a.hasOwnProperty('b')){
   console.log(a.b);
}

Read about it here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty

Duannx
  • 7,501
  • 1
  • 26
  • 59