I'm having trouble with iterating and getting the values within a collection of arrays (an array of arrays I guess)
I'd hope that the code below would display an alert showing the 3 values of each array in turn (eg "infant", "0" and then "2") but the alert just displays "0" "undefined", undefined".
What am I missing?
Declare the array:
var ageGroups = [["infant", 0, 2], ["child", 3, 18], ["child", 3, 17], ["adult1", 18, 64], ["adult2", 65, 74], ["adult3", 75, 79]];
Iterate the array
for (var item in ageGroups) {
alert(item[0]);
alert(item[1]);
alert(item[2]);
}
I'm having trouble with iterating and getting the values within a collection of arrays (an array of arrays I guess)
I'd hope that the code below would display an alert showing the 3 values of each array in turn (eg "infant", "0" and then "2") but the alert just displays "0" "undefined", undefined".
What am I missing?
Declare the array:
var ageGroups = [["infant", 0, 2], ["child", 3, 18], ["child", 3, 17], ["adult1", 18, 64], ["adult2", 65, 74], ["adult3", 75, 79]];
Iterate the array
for (var item in ageGroups) {
alert(item[0]);
alert(item[1]);
alert(item[2]);
}
Share
Improve this question
asked Apr 4, 2012 at 12:31
BrightonDevBrightonDev
4352 gold badges7 silver badges21 bronze badges
1
- anyway what you need to know is the for in loop returns item as a string , the name of the current property the loop is looking up , not an object or an array , so for in is not a foreach you can find in other langages , there is a forEach function for arrays in ES5 though – mpm Commented Apr 4, 2012 at 13:07
7 Answers
Reset to default 4use console.log
instead of alert, Alert will show just [Object ], if variable is a object but in console you can see what kind of object and you can debug further
for (var item in ageGroups) {
console.log(ageGroups[item][0]);
console.log(ageGroups[item][1]);
console.log(ageGroups[item][2]);
}
for (var item in ageGroups) {
alert(ageGroups[item][0]);
alert(ageGroups[item][1]);
alert(ageGroups[item][2]);
}
your porblem is that item is the key of your array
try this:
for (var item in ageGroups) {
alert(ageGroups[item][0]);
alert(ageGroups[item][1]);
alert(ageGroups[item][2]);
}
Use the damn forEach
! :-) Not cross-browser though, but the shim is easy to implement.
// Call forEach and define the callback function
ageGroups.forEach(loopArray)
// Now let's work with the array!
function loopArray(ageGroup) {
console.log(ageGroup[0])
console.log(ageGroup[1])
console.log(ageGroup[2])
}
You should do
for (i = 0; i <ageGroups.length; i++) {
var item = ageGroups[i];
alert(item[0]);
alert(item[1]);
alert(item[2]);
}
for..in
in javascript is used to iterate over the properties of an object
Here's an optimized for
loop, I'm storing the length here so it doesn't evaluate at every iteration:
for (var i = 0, l = ageGroups.length; i < l; i++){
alert(ageGroups[i][0]);
alert(ageGroups[i][1]);
alert(ageGroups[i][2]);
}
to make it exactly like your example you can store the ageGroup's iteration in a variable:
for (var i = 0, l = ageGroups.length, item; i < l; i++){
item = ageGroups[i];
alert(item[0]);
alert(item[1]);
alert(item[2]);
}
Don't use for in
to iterate arrays in JavaScript. Its purpose is to iterate over object properties. Instead use an incremental for loop..
for (var i=0; i<ageGroups.length; i++) {
for (var j=0; j<ageGroups[i].length; j++) {
console.log(ageGroups[i][j]);
}
// Or instead of an inner loop access them individually
console.log(ageGroups[i][0]);
console.log(ageGroups[i][1]);
console.log(ageGroups[i][2]);
}
See it in action...
Using the for-in
construct on an array can produce drastically different results than an incremental loop if, for example, you defined only one array item like myArr[3] = 123;
. In that case, JavaScript will allocate items 0-2, and the for loop will iterate them, but the for-in
won't. More importantly, external scripts and frameworks may extend the Array prototype and add properties which will suddenly be included in your for-in
iterator when you really just wanted the array elements.