Each function created inside the loop closes over the same FilePath
(i.e. they don't each get their own copy), which means that they'll each see whatever value that variable has whenever they execute. To make this work how you expect, you need to arrange it so that they do each get their own copy. Since JavaScript variables are scoped to the nearest enclosing function, the way to do that is to wrap the function creation in an immediately-invoked function that receives the desired value as an argument:
(function(FilePath) {
parser.on('metadata', function (result) {
if (result.picture.length == 0) {
$("#track-table").append('<tr path="'+ FilePath +'"><td></td></tr>');
} else {
var picture = base64ArrayBuffer(result.picture[0].data);
$("#track-table").append('<tr path="'+ FilePath +'"><td></td></tr>');
}
});
})(FilePath);
In this case, each new function closes over a new FilePath
, which produces the wanted result.