循环的JavaScript json语法错误

JavaScript json for-loop Syntax Error

本文关键字:语法 错误 json JavaScript 循环      更新时间:2023-09-26

我正在尝试消除一组变量:

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...}
    ]

循环使用:

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

为了消除所有这些变量,我尝试以以下方式编辑for-loop

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

我遇到了一个语法错误,我不确定我做得是否正确。举个例子将不胜感激。

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

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