I created arrays of random numbers in Python and reading them in Javascript via .txt files. The final product of reading the files is a 3 dimensional array, whenever I log this array I get my expected result, but if I try to index it undefined is returned.
document.getElementById('inputfile').addEventListener('change',
function (){
var c_file_=[]
var fr=[];
var blocks=[];
for(x=0;x<this.files.length;x++){
fr.push(new FileReader());
const f=x;
fr[f].readAsText(this.files[f])
fr[f].onloadend=function(){
file=(fr[f].result)
if(file==null){
console.log('read_fail')
c_file_.push(file_[f-1])
}else{
blocks=file.split("\n")
string=[]
for(b=0;b<blocks.length;b++){
string.push(blocks[b].split("], ["))
string[b][0]=string[b][0].replaceAll('[[',"")
string[b][string[b].length-1]=string[b][string[b].length-1].replaceAll(']]',"")
}
values=[]
for(i=0;i<string.length;i++){
values.push([])
for(n=0;n<string[i].length;n++){
values[i].push(parseFloat(string[i][n]))
}
}
c_file_.push(values);
}
}
}
console.log(c_file_)
})
This is what gets logged if I log the whole array:
0: (3) [Array(8), Array(4), Array(1)] 1: (3) [Array(8), Array(4), Array(1)] 2: (3) [Array(8), Array(4), Array(1)]
But if I log c_file_[0]
I get undefined.
I've tried using spread to map the array, mapping the array before assigning values, and various cloning methods to avoid memory leaks. Nothing has allowed me to access the value of the final array though. I'm new to javascript so I may be missing something obvious here but I can't make sense of it returning undefined for something that seems to be defined.