This question already has answers here:
Decrementing for loop in coffeescript
(6 answers)
Closed 8 years ago.
I'm trying to implement this loop with Coffeescript
for( var i = arr.length; i--; )
Any ideas?
for i in [arr.length-1..0] by -1
# Something!
Related
This question already has answers here:
How do I create dynamic variable names inside a loop?
(8 answers)
Closed 3 years ago.
Hi I want to change the variable name like stop1, stop2, stop3 etc in a loop.
I tried using for loop with stop + i but it didnt work
Please help
var varMap = {};
for(var i=1; i<10; i++) varMap['stop'+i] = 123;
I think this will help
This question already has answers here:
setTimeout calls function immediately instead of after delay
(2 answers)
JavaScript setTimeout() won't wait to Execute? [duplicate]
(3 answers)
setTimeout in for-loop does not print consecutive values [duplicate]
(10 answers)
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 4 years ago.
can someone help me understanding why this code:
for (var i =0; i < 2; i++) {
setTimeout(console.log(i), 0);
}
console.log("aaa");
Will write:
0
1
aaa
But that code:
for (var i =0; i < 2; i++) {
setTimeout(function(){console.log(i)}, 0);
}
console.log("aaa");
Will wirte that:
aaa
2
2
Note that I understand how the second vers. work, I don't get why the first one make it differnt.
Thanks!
This question already has answers here:
Object.length undefined in javascript [duplicate]
(3 answers)
JavaScript object literal length === undefined?
(5 answers)
Closed 5 years ago.
Here is my jsfiddle
Here is my array looks like
var arr = [];
arr['alpha'] = {'a':'1','b':'1'};
arr['beta'] = {'a':'2','b':'4'};
console.log(arr)
When i take console.log(arr.length) It says 0. So i can't able to for loop this one
How can i iterate this array ?
Note :
I don't want to use jquery, i prefer only javascript
This question already has answers here:
Length of a JavaScript object
(43 answers)
Closed 6 years ago.
I dont see how we can find the length of an object. For arrays i can your array.length but it doesnt work for objects, any suggestions?
Thanks!
Just like that:
Object.keys(objectName).length;
This question already has answers here:
JavaScript Infinitely Looping slideshow with delays?
(11 answers)
Closed 8 years ago.
I want to create an infinite loop in JavaScript.
What are some ways to achieve this:
eg
for (var i=0; i<Infinity; i++) {}
You can also use a while loop:
while (true) {
//your code
}
By omitting all parts of the head, the loop can also become infinite:
for (;;) {}