program story

Node.js : 각… 작동하지 않음

inputbox 2020. 12. 6. 21:32
반응형

Node.js : 각… 작동하지 않음


for each ... inNode.js (v0.4.11)와 함께 사용 하고 싶었습니다 .

다음과 같이 사용합니다.

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

다음과 같은 오류가 발생합니다.

        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)

실수는 어디에 있습니까? for each ... inJavascript 1.6부터 지원됩니다.

의 사용법에 대한 정보는 MDN참조하십시오 for each ... in.


불행히도 노드는 for each ... inJavaScript 1.6에 지정되었지만을 지원하지 않습니다 . Chrome은 동일한 JavaScript 엔진 을 사용하며 유사한 단점 이있는 것으로보고됩니다 .

당신은 정착해야 할 것입니다 array.forEach(function(item) { /* etc etc */ }).

수정 : Google의 공식 V8 웹 사이트에서 :

V8은 ECMA-262에 지정된대로 ECMAScript를 구현합니다 .

for each ...inJavaScript 1.6에 있다고 표시된 동일한 MDN 웹 사이트에서 ECMA 버전이 아니라고 말합니다. 따라서 아마도 Node.js에는없는 것 같습니다.


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

https://github.com/cscott/jsshaper 는 JavaScript 1.8에서 ECMAScript 5.1 로의 변환기를 구현하여 웹킷 또는 노드에서 실행되는 코드에서 'for each'를 사용할 수 있습니다.


for each inNode.js에서 지원하는 ECMAScript 버전 에는 없으며 현재 firefox에서만 지원됩니다.

주목해야 할 중요한 점은 JavaScript 버전은 Gecko (Firefox의 엔진) 및 Rhino (항상 몇 버전 뒤에 있음)에만 관련이 있다는 것입니다. Node는 ECMAScript 사양을 따르는 V8을 사용합니다.


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

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
}

참고URL : https://stackoverflow.com/questions/7184099/node-js-for-each-in-not-working

반응형