63

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

I use it like this:

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 is supported since Javascript 1.6.

See MDN for information about the usage of for each ... in.

Michael Gaskill
  • 7,913
  • 10
  • 38
  • 43
pvorb
  • 7,157
  • 7
  • 47
  • 74

6 Answers6

123

Unfortunately node does not support for each ... in, even though it is specified in JavaScript 1.6. Chrome uses the same JavaScript engine and is reported as having a similar shortcoming.

You'll have to settle for array.forEach(function(item) { /* etc etc */ }).

EDIT: From Google's official V8 website:

V8 implements ECMAScript as specified in ECMA-262.

On the same MDN website where it says that for each ...in is in JavaScript 1.6, it says that it is not in any ECMA version - hence, presumably, its absence from Node.

Community
  • 1
  • 1
S M
  • 8,155
  • 3
  • 35
  • 36
  • 7
    Don't forget `Object.keys` to convert the object's keys to an Array – goatslacker Aug 25 '11 at 06:34
  • Well, if that's how V8 behaves, I'm fine with that. I'm able to use `for ... in` but wanted to have some syntactic sugar. Thank you for the information. – pvorb Aug 25 '11 at 10:49
77
for (var i in conf) {
  val = conf[i];
  console.log(val.path);
}
ace
  • 7,293
  • 3
  • 23
  • 28
6

https://github.com/cscott/jsshaper implements a translator from JavaScript 1.8 to ECMAScript 5.1, which would allow you to use 'for each' in code running on webkit or node.

3

This might be an old qustion, but just to keep things updated, there is a forEach method in javascript that works with NodeJS. Here's the link from the docs. And an example:

     count = countElements.length;
        if (count > 0) {
            countElements.forEach(function(countElement){
                console.log(countElement);
            });
        }
toing_toing
  • 2,334
  • 1
  • 37
  • 79
2

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

The important thing to note is that JavaScript versions are only relevant to Gecko (Firefox's engine) and Rhino (which is always a few versions behind). Node uses V8 which follows ECMAScript specifications

Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • 2
    There _is_ an each. Sort of. `for each` is an alternative syntax that should work as done in the original question, but in practice apparently it is in "JavaScript 1.6" but not in the ECMAScript specification and so isn't always supported. (Though even where it is supported I prefer the syntax from your answer.) – nnnnnn Aug 25 '11 at 01:51
  • Yes, there really is an each: [for each ... in](https://developer.mozilla.org/en/JavaScript/Reference/Statements/for_each...in) – pvorb Aug 25 '11 at 10:53
  • 1
    @nnnnnn There is one, but not for EcmaScript. You should always be careful when using MDN, since its version, JavaScript, is not widely supported – Ruan Mendes May 05 '12 at 00:01
0

for those used to php:

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

usage:

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

similar to php version:

foreach(myArray as i=>v){
  //run code here
}
SwiftNinjaPro
  • 787
  • 8
  • 17
  • If you iterate through an array with this function, `length` property will be included as well, so you need to guard against it. – Genhis Jul 07 '19 at 19:49