简体   繁体   English

我无法访问角度2中的对象内部的对象属性

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

I have an object named contact. 我有一个名为contact的对象。 When I logged the content of this object as console.log(contact) I get the following contents 当我将该对象的内容记录为console.log(contact)我得到以下内容

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 但是当我尝试通过console.log(contact.addresss.length)记录地址长度字段时,我undefined

When I logged the object as console.log(JSON.stringify(contact)) I get the following result 当我将对象记录为console.log(JSON.stringify(contact)) ,得到以下结果

{"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. 当您通过console.log记录对象时, console.log显示的值将与该调用时的值不同,而是您看到该值时的最后一个值。 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. 记录JSON表示时,您记录的是简单字符串,该字符串按值传递,因此您可以看到调用时的值。

So the answer is, that the addresss array is probably added later that you are expecting. 因此,答案是,您可能期望稍后添加addresss数组。 Btw address spells with double s , not triple :] 顺便说一下address双法术s ,不三重:]

Btw same applies to those object fields, that are also not present in the JSON: internet_calls , special_days , websites . 顺便说一句同样适用于那些对象的字段,这也是不存在的JSON: internet_callsspecial_dayswebsites

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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