admin管理员组文章数量:1345306
//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".
//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".
Share Improve this question edited Jan 5, 2016 at 11:46 Lucky 17.4k19 gold badges120 silver badges156 bronze badges asked Oct 24, 2011 at 9:39 SiriusKoderSiriusKoder 3412 gold badges4 silver badges12 bronze badges 6- Can you specify a little more? What are you trying to achieve? – Jørgen Commented Oct 24, 2011 at 9:43
-
Make sure you declare
x
first... – Matt Commented Oct 24, 2011 at 9:44 - 3 for a better reference look here: developer.mozilla/en/JavaScript/Reference/Statements/… and why so, look here: w3fools. – Yoshi Commented Oct 24, 2011 at 9:45
-
Avoid using for in. Use
for(var i=0;i<person.length;i++)
instead. – OptimusCrime Commented Oct 24, 2011 at 10:24 - 1 Could you explain why? @OptimusCrime – Alexander Mistakidis Commented Dec 11, 2012 at 15:53
2 Answers
Reset to default 6You 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.
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:
alert(person["fname"]);
Here fname
is stored in X
along with other keys such as lname
and age
.
本文标签: for loopjavascript quotfor (x in y)quot statementsStack Overflow
版权声明:本文标题:for loop - javascript "for (x in y)" statements - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743812084a2543256.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论