admin管理员组文章数量:1278985
I have an array of objects which will be the basis for a certain menu in my website. It will be build using JavaScript:
[
{"menuName":"Contact Info","sectionName":"contacts"},
{"menuName":"Facilities","sectionName":"facilities"},
{"menuName":"Locations","sectionName":"locations"},
{"menuName":"Packages","sectionName":"packages"},
{"menuName":"Policies","sectionName":"policies"},
{"menuName":"Reviews","sectionName":"reviews"},
{"menuName":"Rooms","sectionName":"rooms"}
]
So I decided to use the "for in loop" so that I won't have to deal with indexes and lengths. I expect seven items to appear in the menu when it gets built (I'll be using <ul>
and <li>
).
When I was debugging and accidentally added a background color to the <li>
, is when all hell broke loose. I found at least 30 empty <li>
after the visible 7th menu <li>
.
Why is this happening?
EDIT:
Here's the loop. The loop creates another object for another function to parse later on. (It creates an <li>
with an <a>
inside with properties provided by the previous array.) I know that the other function works fine because when I change this "for-in" loop to an ordinary for loop, or while loop, it works fine.
this.sectionList = function(menu, id) {
var list = new Array();
for(var i in menu) {
var listItem = {
"element" : "li",
"contains" : [{
"element" : "a",
"attr" : {
"href" : menu[i].sectionName + ':' + id
},
"contains" : menu[i].menuName
}]
}
list.push(listItem);
}
}
I have an array of objects which will be the basis for a certain menu in my website. It will be build using JavaScript:
[
{"menuName":"Contact Info","sectionName":"contacts"},
{"menuName":"Facilities","sectionName":"facilities"},
{"menuName":"Locations","sectionName":"locations"},
{"menuName":"Packages","sectionName":"packages"},
{"menuName":"Policies","sectionName":"policies"},
{"menuName":"Reviews","sectionName":"reviews"},
{"menuName":"Rooms","sectionName":"rooms"}
]
So I decided to use the "for in loop" so that I won't have to deal with indexes and lengths. I expect seven items to appear in the menu when it gets built (I'll be using <ul>
and <li>
).
When I was debugging and accidentally added a background color to the <li>
, is when all hell broke loose. I found at least 30 empty <li>
after the visible 7th menu <li>
.
Why is this happening?
EDIT:
Here's the loop. The loop creates another object for another function to parse later on. (It creates an <li>
with an <a>
inside with properties provided by the previous array.) I know that the other function works fine because when I change this "for-in" loop to an ordinary for loop, or while loop, it works fine.
this.sectionList = function(menu, id) {
var list = new Array();
for(var i in menu) {
var listItem = {
"element" : "li",
"contains" : [{
"element" : "a",
"attr" : {
"href" : menu[i].sectionName + ':' + id
},
"contains" : menu[i].menuName
}]
}
list.push(listItem);
}
}
Share
Improve this question
edited Nov 3, 2011 at 11:27
Peter Mortensen
31.6k22 gold badges110 silver badges133 bronze badges
asked Aug 7, 2011 at 17:46
JosephJoseph
120k30 gold badges184 silver badges237 bronze badges
6
- 2 who knows? could you show what you are doing with the code you have. – Tomas Jansson Commented Aug 7, 2011 at 17:48
- 1 Could you paste your loop code in please? – Bojangles Commented Aug 7, 2011 at 17:48
- How did you add background color? – avetarman Commented Aug 7, 2011 at 17:48
-
My ESP isn't working today. Please actually explain your issue. There is no
<li>
-generating code in your question, and you should not be iterating over arrays withfor in
. – Lightness Races in Orbit Commented Aug 7, 2011 at 17:52 -
2
for in loops
are for iterating over object members, not for iterating over an array. Is the.length
property really that much to deal with? – Matt Commented Aug 7, 2011 at 17:55
3 Answers
Reset to default 11for in
iterates over object properties. Javascript arrays are just a specific kind of object with some handy properties to help you treat them as just arrays, but they still have internal object properties .. and you don't mean to iterate over these).
So, you shouldn't use for in
to iterate over arrays. This only became evident to you when you added your background colour, but it'll always be the case.
Instead, loop with a counter and array .length
.
Your object gets methods and properties passed by JavaScript itself. Those are methods that every object gets when its created.
You have to use .hasOwnProperty
to find only the properties and methods you assigned to the object!
for(var i in myObject){
if(myObject.hasOwnProperty(i)){
console.log(myObject.i);
}
}
Hope that helps!
Here are two articles that helped me two understand it better:
http://bonsaiden.github./JavaScript-Garden/#object.hasownproperty http://javascriptweblog.wordpress./2011/01/04/exploring-javascript-for-in-loops/
I see no difference between the two ways of iterating your data structure in this jsFiddle: http://jsfiddle/jfriend00/HqLdk/.
There are lots of good reasons why you should not use for (x in array)
on arrays. The main reason is that that type of iteration iterates over the properties of the object, not just the array elements. Those properties can include other properties of the array if any have been added where as the for (var i = 0; i < array.length; i++)
method will never have issues with added properties because by definition, it only iterates over the array elements.
It is somewhat luck that when no additional properties have been added to the array object, that iterating over the properties happens to include just the array elements. The language does not want you to iterate array elements that way. You should iterate arrays with
for (var i = 0; i < array.length; i++)
.
I understand the seduction of the simpler syntax, but it is not the right way to do it.
本文标签: arraysProblems with JavaScript quotfor inquot loopStack Overflow
版权声明:本文标题:arrays - Problems with JavaScript "for in" loop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741257248a2366916.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论