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:
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 |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.
3 Answers
Reset to default 12In 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
版权声明:本文标题:Is there a JavaScript equivalent to Python's for loops? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738638164a2104160.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Array.prototype.forEach
. And you shouldn't usefor ... in
to iterate through an array in JavaScript anyway. – Pointy Commented Jan 15, 2015 at 16:19for..of
loops. – Jonathan Lonowski Commented Jan 15, 2015 at 16:22