javascript“for (x in y)”语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7873673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript "for (x in y)" statements
提问by SiriusKoder
//just copied this code from w3schools
var person={fname:"John",lname:"Doe",age:25};
for (x in person)
{
document.write(person[x] + " ");
}
I want to know that, what I have to assume instead of "x".
我想知道,我必须假设而不是“x”。
采纳答案by Sarfraz
The person
is object and X
is variable used in iteration of the for
loop, you can name it anything other than X
also :). Here X
works as key
of the object for example:
的person
是对象,X
是在迭代变量使用的for
循环,你能说出它比其他任何东西X
也:)。这里X
可以作为key
例如对象:
alert(person["fname"]);
Here fname
is stored in X
along with other keys such as lname
and age
.
Here与其他键一起fname
存储,X
例如lname
和age
。
回答by Duncan MacLeod
You want to have
你想拥有
for ( x in Object.keys(person)) {
console.log(person[x]);
}
this will give you a list of KEYS rather than a list of values.
这将为您提供一个键列表而不是值列表。