admin管理员组文章数量:1026250
Here's my code:
obj = {"TIME":123,"DATE":456}
console.log(obj.TIME);
console.log("---------")
for (var key in obj) {
console.log(key);
console.log(obj.key);
}
It prints as the following:
123
---------
TIME
undefined
DATE
undefined
Why does console.log(obj.key) print as undefined?
I want my code to print out the following, using obj.key to print out the value for each key:
123
---------
TIME
123
DATE
456
How do I do so?
Here's my code:
obj = {"TIME":123,"DATE":456}
console.log(obj.TIME);
console.log("---------")
for (var key in obj) {
console.log(key);
console.log(obj.key);
}
It prints as the following:
123
---------
TIME
undefined
DATE
undefined
Why does console.log(obj.key) print as undefined?
I want my code to print out the following, using obj.key to print out the value for each key:
123
---------
TIME
123
DATE
456
How do I do so?
Share Improve this question asked Jul 21, 2017 at 22:10 bobbob 6395 silver badges25 bronze badges1 Answer
Reset to default 7because there is no key in the object with the name 'key'. obj.key
means you are trying to access a key inside obj with the name key. obj.key
is same as obj['key']
you need to use obj[key]
, like this:
obj = {"TIME":123,"DATE":456}
console.log(obj.TIME);
console.log("---------")
for (var key in obj) {
console.log(key);
console.log(obj[key]);
}
Here's my code:
obj = {"TIME":123,"DATE":456}
console.log(obj.TIME);
console.log("---------")
for (var key in obj) {
console.log(key);
console.log(obj.key);
}
It prints as the following:
123
---------
TIME
undefined
DATE
undefined
Why does console.log(obj.key) print as undefined?
I want my code to print out the following, using obj.key to print out the value for each key:
123
---------
TIME
123
DATE
456
How do I do so?
Here's my code:
obj = {"TIME":123,"DATE":456}
console.log(obj.TIME);
console.log("---------")
for (var key in obj) {
console.log(key);
console.log(obj.key);
}
It prints as the following:
123
---------
TIME
undefined
DATE
undefined
Why does console.log(obj.key) print as undefined?
I want my code to print out the following, using obj.key to print out the value for each key:
123
---------
TIME
123
DATE
456
How do I do so?
Share Improve this question asked Jul 21, 2017 at 22:10 bobbob 6395 silver badges25 bronze badges1 Answer
Reset to default 7because there is no key in the object with the name 'key'. obj.key
means you are trying to access a key inside obj with the name key. obj.key
is same as obj['key']
you need to use obj[key]
, like this:
obj = {"TIME":123,"DATE":456}
console.log(obj.TIME);
console.log("---------")
for (var key in obj) {
console.log(key);
console.log(obj[key]);
}
本文标签: jsonIterating through a javascript object to get keyvalue pairsStack Overflow
版权声明:本文标题:json - Iterating through a javascript object to get key-value pairs - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745623123a2159698.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论