8

I usually script/program using python but have recently begun programming with JavaScript and have run into some problems while working with arrays.

In python, when I create an array and use for x in y I get this:

myarray = [5,4,3,2,1]
for x in myarray:
    print x

and I get the expected output of:

5
4
3
..n

But my problem is that when using Javascript I get a different and completely unexpected (to me) result:

var world = [5,4,3,2,1]
for (var num in world) {
    alert(num);
}

and I get the result:

0
1
2
..n

How can I get JavaScript to output num as the value in the array like python and why is this happening?

Codahk
  • 1,202
  • 3
  • 15
  • 25
  • 1
    duplicate, answered in detail here: http://stackoverflow.com/questions/3010840/loop-through-array-in-javascript – gblazex Jun 13 '11 at 12:40

9 Answers9

10

JavaScript and Python are different, and you do things in different ways between them.

In JavaScript, you really should (almost) always iterate over an array with a numeric index:

for (var i = 0; i < array.length; ++i)
  alert(array[i]);

The "for ... in" construct in JavaScript gives you the keys of the object, not the values. It's tricky to use on an array because it operates on the array as an object, treating it no differently than any other sort of object. Thus, if the array object has additional properties — which is completely "legal" and not uncommon — your loop will pick those up in addition to the indexes of the "normal" array contents.

Pointy
  • 405,095
  • 59
  • 585
  • 614
7

The variable num contains the array item's index, not the value. So you'd want:

alert(world[num])

to retrieve the value

Tak
  • 11,428
  • 5
  • 29
  • 48
6

The for var in... loop in JavaScript puts the keys in the variable instead of the actual value. So when using for var ... you should do something like this:

var world = [5, 4, 3, 2, 1];
for ( var key in world ) {
    var value = world[key];
    alert(key + " = " + value);
}

And note that this way of looping is best used when you're using objects instead of arrays. For arrays use the common:

for ( var i = 0, j = arr.length; i < j; i++ ) { ... }

Or if you're targeting modern browser you can use the forEach-method of arrays:

var arr = [1, 2, 3];
arr.forEach(function(num) {
   alert(num);
});
Erik Rothoff
  • 4,826
  • 9
  • 41
  • 59
1

The for...in loop loops over all key elements; not the values.

I would recommend you to use

for(var i=0; i<arr.length; i++){
  alert(arr[i]);
}
Harmen
  • 22,092
  • 4
  • 54
  • 76
1

When you use the in operator num becomes a key. So simply use this key to get a value out of the array.

var world = [5,4,3,2,1]
for (var num in world) {
    alert(world[num]);
}
Z. Zlatev
  • 4,757
  • 1
  • 33
  • 37
1

try this.

var world = [5,4,3,2,1]
for(var i=0;i<world.length;i++){
    alert(world[i])
}

Because javascript in your case is printing the index of the element, not the value.

JAiro
  • 5,914
  • 2
  • 22
  • 21
1

the result you got is just element index,if you want to get element value

your code should like this

var world = [5,4,3,2,1]
for (var num in world) {
    alert(world[num]);
}
simpleman
  • 148
  • 9
1

The for in iteration in JavaScript works only for the object data type. The way it works is that it lets you iterate over the attributes of an object. arrays are objects in JavaScript, but the for in only works on its attributes, not the array values.

For example you might define an array as such:

var arr = [1,2,3];

And you can assign attributes to this array, because it's actually an object:

arr.foo = "bar";
arr["1"] = 2;

Now when you use the for in iteration method you will be able to iterate over the attributes we just assigned above;

for(var i in arr) console.log(i);

To iterate over the actual array values you need to use the for(var i=0; i<arr.length; i++) construct.

Hope this helps.

Luca Matteis
  • 29,161
  • 19
  • 114
  • 169
1

In javascript it's advised to loop Arrays different from looping Objects. You are using an object loop, which may return unexpected result (for instance if the Array.prototype was extended with custom methods you would iterate those too, and it does't guarantee the order of the array is preserved). There are many ways to loop through an array, using it's index:

// regular
var arr = [1,2,3,4,5]
    ,i
;
for (i=0;i<arr.length;i++) {
  console.log(arr[i]);
}

// using while
var arr = [1,2,3,4,5]
    ,i = 0
;
while ((i = i + 1)<arr.length) {
  console.log(arr[i]);
}

// using while reversed
var arr = [1,2,3,4,5]
    ,i = arr.length
;
while ((i = i - 1) > -1) {
  console.log(arr[i]);
}

Note: Why not use i++ or i--? To avoid confusion, index out of range-errors and to satisfy JSLint

KooiInc
  • 119,216
  • 31
  • 141
  • 177