It is fine to use forEach
, what it is suggesting here is that the function that you are passing to forEach
should be created outside of the loop, something like the following:
var doMagic = function(...) {
// do magic
};
for (var i in data) {
data[i].arr.forEach(doMagic);
}
Creating functions within a loop is discouraged because it is inefficient, the JavaScript interpreter will create an instance of the function per loop iteration. Additional details are provided in JSLint Error Explanations: Don't make functions within a loop.