Papa Parse を使用して csv ファイルを解析しています。「完了」では、オブジェクトの配列である結果を、Papa Parse の解析関数への呼び出しの外部で宣言した変数に代入しようとします。変数は関数内では問題ありませんが、外部では未定義です。文字列と整数でもこれを試しましたが、変数は関数の外ではまだ定義されていません。
var data;
for (var i = 0, f; f = files[i]; i++) {
if (f.type == 'application/csv' || f.type == 'application/vnd.ms-excel' || f.type == 'text/csv') {
Papa.parse(f, {
header: true,
dynamicTyping: false,
complete: function(results) {
console.log("Completed parsing results", results);
data = results.data.slice(0); //I tried other simple values here, such as "test"
console.log("Data after assigned value.", data); //Here the data is correctly assigned to the variable
}
});
console.log("Value of data outside function.", data); //Undefined??
}
}