Is there a JavaScript equivalent for Python loops?

So, I was disappointed to learn that JavaScript is for ( var in array/object)not equivalent to pythons for var in list:.

In JavaScript, you repeat the indices themselves, for example.

0, 
1,
2,
... 

where, as in the case of Python, you repeat the values ​​pointed to by indexes, for example.

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

Is there a standard JavaScript equivalent for the Python loop mechanism?

Denial of responsibility:

I know that the for (var in object) construct is for use in iterating over keys in a dictionary and usually not over array indices. I am asking a specific question regarding the use of cases in which I don't care about order (or very much about speed) and just don't want to use a while loop.

+4
3

, forEach ( )

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

, :

1
0
2
1
3
2
4
3
+6

ECMAScript (ECMAScript6 aka Harmony) :

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

for-of , , , . Python for-in.

+4

, . /

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

, , forEach ... heres a 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));
});
0

All Articles