Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a destructuring assignment to iterate over an es6 map with nodejs 4.1.2

Tags:

node.js

I tried to iterate over an es6 map like described here:

for (var [key, value] of myMap.entries()) {
  console.log(key + " = " + value);
}

When running it with nodejs 4.1.2 and the --harmony flag I get

SyntaxError: Unexpected token [

I could write

for (var arr of myMap.entries()) {
  console.log(arr[0] + " = " + arr[1]);
}

but this is not nice because it is not expressive.

Is there a better way?

like image 589
David Michael Gang Avatar asked Dec 03 '25 18:12

David Michael Gang


1 Answers

This answer is made in accordance with this meta post.


As @Ben Fortune has commented:

Have you tried explicitly using the --harmony_destructuring flag?

Actual documentation on this flag is hard to find, but more information can be found in this GitHub issue about destructuring assignment in node 4.

like image 95
LarsW Avatar answered Dec 06 '25 09:12

LarsW