Reputation: 435
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]);
}
Upvotes: 2
Views: 3798
Reputation: 60787
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])
}
Upvotes: 1
Reputation: 270697
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]);
}
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.
Upvotes: 0
Reputation: 18042
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]);
}
Upvotes: 0
Reputation: 7887
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]);
}
Upvotes: 1
Reputation: 20155
for (var item in ageGroups) {
alert(ageGroups[item][0]);
alert(ageGroups[item][1]);
alert(ageGroups[item][2]);
}
Upvotes: 1
Reputation: 6092
use 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]);
}
Upvotes: 4
Reputation: 76890
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
Upvotes: 0