Gets undefined when trying to access a null value inside a nested object in vue js

458 views Asked by At

I have an object

const data={a:'value1',b:{c:'null'}}

Now when I try to access 'c', I get 'undefined' but I am getting 'b'. I tried accessing like this:

console.log(data.b) returns {c:'null'} but
console.log(data.b.c) returns undefined
1

There are 1 answers

0
Shamsail On

Use this

let {c} = data.b;
console.log(c);

It will resolve your problem.

Update: In vue components, sometimes you are not able to fetch nested object like this

data.b.c

so you have to use this strategy to access nested object attribute. like this

let {c} = data.b