在javascript和变量范围内的foreach函数

时间:2016-01-08 09:42:58

标签: javascript foreach scope

我需要类似的东西:

"use strict"

var arrayToFill = new Array();
var arrayWithData = ["a", "b", "c"];

arrayWithData.forEach(function(entry) {

     arrayToFill[entry] = "something";
});

看起来我的NetBeans说,我对arrayToFill变量范围有疑问。我无法从function(entry)访问它。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

这里的范围没有任何问题。

您只是尝试为数组的select * from mytable where lang like 'lang_2' or lang like 'lang_3' and content != '' and content in (select content from mytable group by content having count(*) > 1); ab 属性分配值 - 这没有意义,因为数组是旨在通过数字属性名称保存有序数据。

如果您然后c结果,它将显示为空数组,因为它只显示带有数字索引的数据。这可能是导致您认为范围错误的原因。

您可以像这样访问数据:

console.log

您可能希望使用对象而不是数组。

"use strict"

var arrayToFill = new Array();
var arrayWithData = ["a", "b", "c"];

arrayWithData.forEach(function(entry) {
console.log(entry);
     arrayToFill[entry] = "something";
});

alert(arrayToFill.a);