【发布时间】:2019-06-25 22:29:14
【问题描述】:
我正在编写一个简单的代码来获取数组中的值。我正在使用array[key] 在for 循环中获取存储在此数组中的值。
var array = ["Foo", "Bar"];
function getValue() {
for (var key = 0; key < array.length; key++) {
console.log(array[value]);
}
}
这个方法很简单,效果很好,但是,我读到这可能会导致安全问题(The Dangers of Square Bracket Notation),并且 ESlint 不能接受它,抛出这个错误:
通用对象注入接收器(security/detect-object-injection)
将变量[key] 检测为左手或右手赋值操作数。 (ESLint reference)
不使用这种方法如何访问数组的值?
我已经阅读了相关问题:Why is it bad pratice calling an array index with a variable?,由于这个问题似乎难以概括,我决定提出一个新的规范问题。
【问题讨论】:
-
既然你在处理数组,试试
Array.forEach(function(value) { console.log(value)})。如果您能说出自己在做什么,我们可以提出更好的建议 -
value定义在哪里? -
@nickzoum
value是用户定义的 -
eslint 到底在抱怨什么?请张贴错误信息。我敢打赌这是关于使用
for in,而不是array[value]。
标签: javascript arrays security