简体   繁体   中英

I can't access object properties that are inside an object in angular 2

I have an object named contact. When I logged the content of this object as console.log(contact) I get the following contents

addresss:[]
company:""
emails:[]
id:3
internet_calls:[]
lat:"10.115730000000001"
lng:"76.461445"
name:"Diji  "
phones:[]
special_days:[]
timestamp:1508919658000
title:"Mr"
websites:[]
__proto__:Object

But when i tried to log the length of addresss field by console.log(contact.addresss.length) I get undefined

When I logged the object as console.log(JSON.stringify(contact)) I get the following result

{"id":3,"title":"Mr","name":"Diji  ","company":"","lat":"10.115730000000001","lng":"76.461445","timestamp":1508919658000,"phones":[],"emails":[]}

so my question is why some of the properties of my object is not showing in later log but show up in first log?

When you log object via console.log , the value shown in the console will not be the same as in time of that call, but rather the last value by the time you see it. This is caused by passing object as a reference.

When you log the JSON representation, you log simple string, which is passed by value, so you see the value as it was in the time of call.

So the answer is, that the addresss array is probably added later that you are expecting. Btw address spells with double s , not triple :]

Btw same applies to those object fields, that are also not present in the JSON: internet_calls , special_days , websites .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM