Javascript Node.js:对于每个……不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7184099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:14:01  来源:igfitidea点击:

Node.js: for each … in not working

javascriptnode.jsforeachv8

提问by pvorb

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

我想for each ... in与 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 ... inis supported since Javascript 1.6.

错误在哪里?for each ... in自 Javascript 1.6 起支持。

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

有关使用的信息,请参阅MDNfor each ... in

回答by S M

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 ashaving a similar shortcoming.

不幸的是 node 不支持for each ... in,即使它是在 JavaScript 1.6 中指定的。Chrome 使用相同的 JavaScript 引擎,并被报告有类似的缺点。

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

你必须适应array.forEach(function(item) { /* etc etc */ })

EDIT: From Google's official V8 website:

编辑:来自 Google 的官方 V8 网站:

V8 implements ECMAScript as specified in ECMA-262.

V8 实现了ECMA-262 中指定的ECMAScript

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

在同一个 MDN 网站上,它说它for each ...in在 JavaScript 1.6 中,它说它不在任何 ECMA 版本中——因此,据推测,它不在 Node.js 中。

回答by ace

for (var i in conf) {
  val = conf[i];
  console.log(val.path);
}

回答by C. Scott Ananian

https://github.com/cscott/jsshaperimplements 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.

https://github.com/cscott/jsshaper实现了从 JavaScript 1.8 到 ECMAScript 5.1 的翻译器,这将允许您在 webkit 或节点上运行的代码中使用“for each”。

回答by toing_toing

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:

这可能是一个老问题,但只是为了保持更新,javascript 中有一个 forEach 方法可以与 NodeJS 一起使用。这是文档中的链接。和一个例子:

     count = countElements.length;
        if (count > 0) {
            countElements.forEach(function(countElement){
                console.log(countElement);
            });
        }

回答by Juan Mendes

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

for each inNode.js 支持的 ECMAScript 版本中没有,目前只有 firefox 支持。

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

需要注意的重要一点是,JavaScript 版本仅与 Gecko(Firefox 的引擎)和 Rhino(总是落后几个版本)相关。Node 使用遵循 ECMAScript 规范的 V8

回答by SwiftNinjaPro

for those used to php:

对于那些习惯使用 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:

类似于 php 版本:

foreach(myArray as i=>v){
  //run code here
}