无法在nodejs中看到对象的属性

时间:2018-04-30 18:43:57

标签: javascript node.js

我开始使用nodejs。 我有一个对象,我想看到我的对象的属性

all:
children:
primary:
hosts:
jules:
ansible_port: 22
ansible_host: xxxx
ansible_user: root
ansible_ssh_private_key_file: /etc/openstack-helm/deploy-key.pem
ansible_ssh_extra_args: -o StrictHostKeyChecking=no
nodes:
hosts:
verne:
ansible_port: 22
ansible_host: xxxx
ansible_user: root
ansible_ssh_private_key_file: /etc/openstack-helm/deploy-key.pem
ansible_ssh_extra_args: -o StrictHostKeyChecking=no

我尝试使用JSON.parse(JSON.stringify(options)和options [property] .....没有

console.log( typeof options )  // object 
console.log("this.optionsCode 2 ",  options ) 
// object = { data : 0, codeName : 'test' }
console.log("this.optionsCode 2 ",  options.codeName )
// undefined

完整的结果

console.log("option type ",  typeof options )
console.log("option Object.keys(options) ",  Object.keys(options) )
console.log("options ",  options )
console.log(" options.codeName ",  options.codeName )
console.log("JSON.stringify(options) ",  JSON.stringify(options) )                                      console.log("JSON.stringify(options.codeName) ",  JSON.stringify(options.codeName) )

1 个答案:

答案 0 :(得分:3)

您正在尝试访问嵌套属性。所以请使用options.leagueCaption.codeName

var options= { leagueCaption:
   { country: 'FR',
     code: '451',
     codeName: 'LEAGUE_2',
     leagueCaption: 'Ligue 2 2017/18' },
  matchday: 32 };
  
  console.log(options.leagueCaption.codeName)