Nodo.js: para cada ... en no trabajar


Quería usar for each ... in con Node.js (v0.4.11).

Lo uso así:

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

Obtengo el siguiente 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)

¿Dónde está el error? for each ... in está soportado desde Javascript 1.6.

Ver MDN para obtener información sobre el uso de for each ... in.

Author: Michael Gaskill, 2011-08-25

5 answers

Desafortunadamente node no soporta for each ... in, a pesar de que está especificado en JavaScript 1.6. Chrome utiliza el mismo motor JavaScript y se informa que tiene un defecto similar.

Tendrás que conformarte con array.forEach(function(item) { /* etc etc */ }).

EDITAR: Desde el sitio web oficial de Google V8:

V8 implementa ECMAScript como se especifica en ECMA-262.

En el mismo sitio web de MDN donde dice que for each ...in está en JavaScript 1.6, dice que no está en ninguna ECMA versión-de ahí, presumiblemente, su ausencia del Nodo.

 109
Author: Sebastian Motraghi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:25:48
for (var i in conf) {
  val = conf[i];
  console.log(val.path);
}
 64
Author: ace,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-08-25 01:26:10

Https://github.com/cscott/jsshaper implementa un traductor de JavaScript 1.8 a ECMAScript 5.1, que le permitiría usar 'for each' en código que se ejecuta en webkit o node.

 6
Author: C. Scott Ananian,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-16 23:34:08

No hay for each in en la versión de ECMAScript soportada por Node.js, solo soportado por firefox actualmente.

Lo importante a tener en cuenta es que las versiones de JavaScript solo son relevantes para Gecko (el motor de Firefox) y Rhino (que siempre está un par de versiones atrás). Node utiliza V8 que sigue las especificaciones de ECMAScript

 2
Author: Juan Mendes,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-17 12:06:02

Esto puede ser un qustion antiguo, pero solo para mantener las cosas actualizadas, hay un método forEach en javascript que funciona con NodeJS. Aquí está el enlace de los documentos . Y un ejemplo:

     count = countElements.length;
        if (count > 0) {
            countElements.forEach(function(countElement){
                console.log(countElement);
            });
        }
 2
Author: toing_toing,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-06-28 14:07:50