Node.js: for everyone ... inoperative

I wanted to use for each ... in with Node.js (v0.4.11).

I use it as follows:

 var conf = { index: { path: { first: "index.html", pattern: "index/{num}.html" }, template: "index.tpl", limit: 8 }, feed: { path: "feed.xml", template: "atom.tpl", limit: 8 } } for each (var index in conf) { console.log(index.path); } 

I get the following error:

  for each (var index in conf) { ^^^^ node.js:134 throw e; // process.nextTick error, or 'error' event on first tick ^ SyntaxError: Unexpected identifier at Module._compile (module.js:397:25) at Object..js (module.js:408:10) at Module.load (module.js:334:31) at Function._load (module.js:293:12) at require (module.js:346:19) at Object.<anonymous> (/home/paul/dev/indexing/lib/Index.js:3:13) at Module._compile (module.js:402:26) at Object..js (module.js:408:10) at Module.load (module.js:334:31) at Function._load (module.js:293:12) 

Where is the mistake? for each ... in supported since Javascript 1.6.

For information on using for each ... in see MDN .

+58
javascript foreach v8
Aug 25 '11 at 1:16
source share
6 answers

Unfortunately, node does not support for each ... in , although it is specified in JavaScript 1.6. Chrome uses the same JavaScript mechanism and is reported as having a similar flaw.

You need to agree to array.forEach(function(item) { /* etc etc */ }) .

EDIT: from the official Google V8 website:

V8 implements ECMAScript as specified in ECMA-262 .

On the same MDN website, which says that for each ...in is in JavaScript 1.6, he says that he is not in any version of ECMA - hence, apparently, his absence from Node.

+118
Aug 25 '11 at 1:23
source share
 for (var i in conf) { val = conf[i]; console.log(val.path); } 
+67
Aug 25 '11 at 1:26
source share

https://github.com/cscott/jsshaper embeds a translator with JavaScript 1.8 in ECMAScript 5.1, which allows you to use "for everyone" in code running on a website or node.

+6
Feb 16 2018-12-12T00:
source share

There is no for each in version in the ECMAScript version supported by Node.js, only currently supported by firefox.

It’s important to note that JavaScript versions only apply to Gecko (Firefox engine) and Rhino (which always has multiple versions). Node uses a V8 that follows ECMAScript specifications

+2
Aug 25 2018-11-21T00:
source share

This may be an old version, but just to update the information, javascript has a forEach method that works with NodeJS. Here is a link from the documentation . And an example:

  count = countElements.length; if (count > 0) { countElements.forEach(function(countElement){ console.log(countElement); }); } 
+2
Jun 28 '18 at
source share

for those who used php:

 //add this function function foreach(arr, func){ for(var i in arr){ func(i, arr[i]); } } 

using:

 foreach(myArray, function(i, v){ //run code here }); 

similar to php version:

 foreach(myArray as i=>v){ //run code here } 
0
Jul 07 '19 at 19:07
source share



All Articles