admin管理员组

文章数量:1201992

So I was dissapointed to find out that JavaScript's for ( var in array/object) was not equivalent to pythons for var in list:.

In JavaScript you are iterating over the indices themselves e.g.

0, 
1,
2,
... 

where as with Python, you are iterating over the values pointed to by the indices e.g.

"string var at index 0", 
46, 
"string var at index 2",
["array","of","values"],
...

Is there a standard JavaScript equivalent to Python's looping mechanism?

Disclaimer:

I am aware that the for (var in object) construct is meant to be used to iterate over keys in a dictionary and not generally over indices of an array. I am asking a specific question that pertains to use cases in which I do not care about order(or very much about speed) and just don't feel like using a while loop.

So I was dissapointed to find out that JavaScript's for ( var in array/object) was not equivalent to pythons for var in list:.

In JavaScript you are iterating over the indices themselves e.g.

0, 
1,
2,
... 

where as with Python, you are iterating over the values pointed to by the indices e.g.

"string var at index 0", 
46, 
"string var at index 2",
["array","of","values"],
...

Is there a standard JavaScript equivalent to Python's looping mechanism?

Disclaimer:

I am aware that the for (var in object) construct is meant to be used to iterate over keys in a dictionary and not generally over indices of an array. I am asking a specific question that pertains to use cases in which I do not care about order(or very much about speed) and just don't feel like using a while loop.

Share Improve this question edited Jan 15, 2015 at 16:39 Luke asked Jan 15, 2015 at 16:18 LukeLuke 5,7085 gold badges38 silver badges68 bronze badges 4
  • 3 There's Array.prototype.forEach. And you shouldn't use for ... in to iterate through an array in JavaScript anyway. – Pointy Commented Jan 15, 2015 at 16:19
  • Why? how much slower is it? I don't care about order. – Luke Commented Jan 15, 2015 at 16:22
  • Going forward, probably for..of loops. – Jonathan Lonowski Commented Jan 15, 2015 at 16:22
  • 1 It's unidiomatic and it has the potential of introducing weird bugs. JavaScript is a different programming language than Python. – Pointy Commented Jan 15, 2015 at 16:24
Add a comment  | 

3 Answers 3

Reset to default 12

In the next version of ECMAScript (ECMAScript6 aka Harmony) will be for-of construct:

for (let word of ["one", "two", "three"]) {
  alert(word);
}

for-of could be used to iterate over various objects, Arrays, Maps, Sets and custom iterable objects. In that sense it's very close to Python's for-in.

for an array the most similar is the forEach loop (of course index is optional)

[1,2,3,4,].forEach(function(value,index){
  console.log(value);
  console.log(index);
});

So you will get the following output:

1
0
2
1
3
2
4
3

I'm not sure I see MUCH difference. It's easy to access the value at a given index/key

var list = [1,2,3,4,5];

// or...

var list = {a: 'foo', b: 'bar', c: 'baz'};
for (var item in list) console.log(list[item]);

and as mentioned, you could use forEach for arrays or objects... heres an obj:

var list = {a: 'foo', b: 'bar', c: 'baz'}; 

Object.keys(list).forEach(function(key, i) {
    console.log('VALUE: \n' + JSON.stringify(list[key], null, 4));
});

本文标签: Is there a JavaScript equivalent to Python39s for loopsStack Overflow