I have a function where I need to create one anonymous functions per item in an array. I am parsing a CSV file and I want to create an anonymous function to process each row (so I can use async.paralell).
See the following fiddle to get an idea what I am trying to do:
var arr = [1, 2, 3];
var func_arr = [];
var str = 'Output: ';
for (var i = 0, len = arr.length; i < len; i++) {
var aFunc = function () {
str = str + ' ' + i.toString();
document.getElementById('text').innerHTML = str;
}
func_arr.push(aFunc);
}
for (var x = 0; x < func_arr.length; x++) {
var func = func_arr[x];
func();
}
https://jsfiddle.net/tg1fpjy7/4/ (the expected output should be 1 2 3)
Here is one way I solved it using another variable to track position again : https://jsfiddle.net/pk0Lc16L/1/ .. however, I hate the solution and there must be a more elegant way of doing this.
Thoughts?
Thanks!
Rob