最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

oop - Accessing object properties in javascript using for loop? - Stack Overflow

programmeradmin6浏览0评论

consider the following object:

var nyc = {
fullName: "New York City",
mayor: "Bill de Blasio",
population: 8000000,
boroughs: 5
};

when i try to access each of the properties using for loop:

for(var key in nyc){
console.log(nyc[key]);
}

it returns correct output(property values), but...

for(var key in nyc){
console.log(nyc.key);
}

this return "undefined" on 4 lines

Why the strange behavior, since both:

console.log(nyc.fullName);
console.log(nyc['fullName']);

give same o/p.

consider the following object:

var nyc = {
fullName: "New York City",
mayor: "Bill de Blasio",
population: 8000000,
boroughs: 5
};

when i try to access each of the properties using for loop:

for(var key in nyc){
console.log(nyc[key]);
}

it returns correct output(property values), but...

for(var key in nyc){
console.log(nyc.key);
}

this return "undefined" on 4 lines

Why the strange behavior, since both:

console.log(nyc.fullName);
console.log(nyc['fullName']);

give same o/p.

Share Improve this question asked Jul 18, 2015 at 8:16 purudpdpurudpd 1792 silver badges15 bronze badges 5
  • possible duplicate of JavaScript property access: dot notation vs. brackets? – Kyll Commented Jul 18, 2015 at 8:18
  • @Kyll: No, that's asking for reasons to use one or the other, explicitly "other than the obvious" that you can use a variable with one of them. I'm sure there's a dupe (dozens, probably, all subtly different), but that's not it. – T.J. Crowder Commented Jul 18, 2015 at 8:22
  • But don't the answers there actually answer this question? I read on Meta that I should flag as a dupe for the sake of the answer, not of the question. – Kyll Commented Jul 18, 2015 at 8:24
  • @Kyll: I'd say no, they talk about reasons for using one or the other, not the fundamental. (In particular the accepted/upvoted one.) But it's probably a judgment call. I'm sure there's a good original out there to point this at, though. If not, this is one of the purest versions of this question I've seen, it could bee the canonical one. (But there must be one.) – T.J. Crowder Commented Jul 18, 2015 at 8:28
  • Then, if something more specific is needed, stackoverflow./questions/20736758/… should do the trick no? Already spent my flag though. – Kyll Commented Jul 18, 2015 at 8:32
Add a ment  | 

1 Answer 1

Reset to default 6

nyc.key looks for the property with the name key, not the property with the name in the variable key. Your first example, nyc[key], is the correct way to use the property name from the variable.

In JavaScript, you can access object properties using dot notation and a property name literal (obj.foo), or brackets notation and a property name string (obj["foo"]). In that second case, you can use any expression to get the string, including a variable lookup. (In ES6, you can also use Symbols with brackets notation, but it's not relevant here.)

发布评论

评论列表(0)

  1. 暂无评论