JavaScript json for-loop语法错误

时间:2022-06-16 22:44:23

I am trying to eliminate a chunk of variables:

我试图消除一大块变量:

var security  = [
      { name:   'X-Powered-By',
        option: '' }
    , { name:   'x-frame-options',
        option: file.get('headers.xFrameOptions') }
    , { name:   'X-XSS-Protection',
        option: file.get('headers.xXSSProtection') }
    , { // AND SO ON...}
    ]

That is looped with:

这循环:

// Add Content Security Rules to the header
for(var i = 0; i < security.length; i++) {
  res.setHeader(security[i].name, security[i].option);
}

In order to eliminate all those variables, I am trying to edit the for-loop in the following way:

为了消除所有这些变量,我试图以下列方式编辑for循环:

for(var i = 0; i < file.get('headers.length; i++') {
  res.setHeader(headers[i].name);
}

I am getting a syntax error and I am not sure if I am doing it correctly. An example would be very appreciated.

我收到语法错误,我不确定我是否正确执行此操作。一个例子将非常感激。

1 个解决方案

#1


0  

you have problems with your closing brackets and quotes, should be:

你的右括号和引号有问题,应该是:

for(var i = 0; i < file.get('headers.length') ; i++) {
  res.setHeader(headers[i].name);
}

#1


0  

you have problems with your closing brackets and quotes, should be:

你的右括号和引号有问题,应该是:

for(var i = 0; i < file.get('headers.length') ; i++) {
  res.setHeader(headers[i].name);
}