Codahk
Codahk

Reputation: 1220

JavaScript Array

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?

Upvotes: 7

Views: 763

Answers (9)

KooiInc
KooiInc

Reputation: 123026

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

Upvotes: 1

Luca Matteis
Luca Matteis

Reputation: 29267

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.

Upvotes: 1

simpleman
simpleman

Reputation: 146

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]);
}

Upvotes: 1

JAiro
JAiro

Reputation: 6009

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.

Upvotes: 1

Z. Zlatev
Z. Zlatev

Reputation: 4820

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]);
}

Upvotes: 1

Harmen
Harmen

Reputation: 22466

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]);
}

Upvotes: 1

Erik Rothoff
Erik Rothoff

Reputation: 5143

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);
});

Upvotes: 6

Tak
Tak

Reputation: 11742

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

alert(world[num])

to retrieve the value

Upvotes: 7

Pointy
Pointy

Reputation: 414086

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.

Upvotes: 10

Related Questions