I working on an Angular project which I have to upload a .txt
file then parse all its lines loop over them. I used this peace of code but it just returns me a text format just like opening it in notepad and that’s not what I want, my goal is to every single data with the delimiter ;
and console that in an array ob objects.
this is my code:
JavaScript
x
10
10
1
fileChangeListener($event: any): void {
2
const file = $event.target.files[0];
3
let fileReader = new FileReader();
4
fileReader.onload = (e) => {
5
let data = fileReader.result;
6
console.log("FileREAAAAAAAAAAADER n" + data);
7
8
}
9
fileReader.readAsText(file);
10
this is my .txt
file structure:
JavaScript
1
6
1
1234;06/07/22;06/07/22;VRT; ;31070;some String content;some String content; ;147.10;A;1234
2
1234;06/07/22;06/07/22;VRT; ;31070;some String content;some String content; ;147.10;A;1234
3
1234;06/07/22;06/07/22;VRT; ;31070;some String content;some String content; ;147.10;A;1234
4
1234;06/07/22;06/07/22;VRT; ;31070;some String content;some String content; ;147.10;A;1234
5
1234;06/07/22;06/07/22;VRT; ;31070;some String content;some String content; ;147.10;A;1234
6
in console, the code I wrote displays just like the above structure where the output should be like this:
Advertisement
Answer
I have modified the your code to make a string[][] like you needed.
Not knowing what you want to do with the data, it is just local to that function.
dummyArr is what you want 🙂
Kept it kinda bland so you can modify it to your future needs
Hope this helps!
JavaScript
1
34
34
1
fileChangeListener(event: any): void {
2
console.log("submitted here")
3
const file = event.target.files[0];
4
let fileReader = new FileReader();
5
fileReader.onload = (e) => {
6
let data = fileReader.result;
7
console.log("FileREAAAAAAAAAAADER n" + data);
8
this.parseData(data)
9
}
10
fileReader.readAsText(file);
11
}
12
13
parseData(data: string | ArrayBuffer | null){
14
var dummyArr: string[][] = []
15
var eachLine = data?.toString().split('n');
16
eachLine?.forEach((line: string) => {
17
let arr = []
18
let str = ""
19
for(var i = 0; i < line.length; i++){
20
if(line[i] == ';'){
21
arr.push(str)
22
str = ""
23
}else{
24
str += line[i]
25
}
26
}
27
arr.push(str)
28
dummyArr.push(arr)
29
})
30
console.log(dummyArr);
31
}
32
33
34