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.
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
How to convert JavaScript `for` loop to Python? - Stack Overflow
for loop in Python and for loop in Java: differences
Can someone please help a beginner with a loop?
Asynchronous python vs asynchronous javascript
Videos
for loops in Python
It can be easily converted using range() or xrange() function. The xrange() is an iterator version, which means it is more efficient (range() would first create a list you would be iterating through). Its syntax is the following: xrange([start], stop[, step]). See the following:
for y in xrange(0, 128, 1024):
for x in xrange(0, 64, 1024):
# here you have x and y
Note
But I hope you have noticed, that due to the fact, that you are incrementing y and x with each respective loop by 1024, you will actually receive something similar to this:
var y = 0;
var x = 0;
or, in Python:
x = 0
y = 0
Anyway, it is just additional note about the code you have given as example.
Your code will only perform one iteration in each loop, so you don't even need a loop:
y = 0
x = 0
# do whatever with x and y here
In general, you can use range([start], stop[, step]) [docs] to simulate such a for loop.
For example:
for(var i = 0; i < 10; i += 2)
becomes
for i in range(0, 10, 2)