我正在从注册表中读取键值。它会返回一个javascript对象,如下所示,
我无法访问该值。
I tried to print the value as
path="HKLM\\System\\CurrentControlSet\\Control\\Session Manager\\Environment";
console .log(result.path.values.USERNAME);//prints undefined
答案 0 :(得分:1)
尝试以下代码:
result[path].values.USERNAME // array syntax
在JavaScript中,当您使用“常规对象语法”时,将变量用作Object的属性名称没有用,它将返回undefined(如果它没有具有与您的变量的确切名称相同的属性):< / p>
const obj = {
key1: 'value1',
key2: 'value2'
}
let prop = 'key1'
// this returns undefined,
// as obj doesn't have a prop1 property
console.log('obj.prop:', obj.prop)
// this display value1, because an Object
// is an Array (under the hood), so you can
// call it's keys in an array syntax
console.log('obj[prop]:', obj[prop])
// if the prop variable gets a new value, then
// it will return a value from obj according to
// prop's new value
prop = 'key2'
console.log('prop = \'key2\':', obj[prop])
// you can use the array syntax for listing out
// the property values of an Object:
for (let key in obj) {
console.log('iteration ' + key + ':', obj[key])
}