collecting data from a text file and parsing the collected data to create an array of objects

Issue

I need some help
So basically I have to collect some data from a text file placed on GitHub (some data about some students that looks like this

john
New York
123456456
Math
Ben
California
3265455554
Philosophy
…..
…..
…..

there are in total 5 students), and I have to parse the file in to the web page like this using either a class or object

Name: John
Address: New York
Phone: 123456456
Course: Math

Name: Ben
Address: California
Phone: 3265455554
Course: Philosophy

Name: ….
Address: ….
Phone:…..
Course: ….

and so on.

The idea is to use async /await functions in my assignment this is what my teacher told me.

var button = document.getElementById("btn");
var loader = document.getElementById("loading");
var displayArea = document.getElementById("data");


button.addEventListener("click", function () {


  getData();



})

class Student {

  constructor(fullName, address, phone, course) {

    this.fullName = fullName;
    this.address = address;
    this.phone = phone;
    this.course = course;

  }

  getInfo() {
    return "Full Name: " + this.fullName + "\n" +
      "Address: " + this.address + "\n" +
      "Telephone: " + this.phone + "\n" +
      "Course: " + this.course;
  }
}

var studentName = new Student().fullName;
var studentAddress = new Student().address;
var studentPhone = new Student().phone;
var studentCourse = new Student().course;
var studentInfo = new Student().getInfo();

var studentData = [studentName, studentAddress, studentPhone, studentCourse]


async function getData() {

  loader.style.display = "inline-block";


  try {

    let response = await fetch(`https://v-dresevic.github.io/Advanced-JavaScript-Programming/data/students.txt`);


    if (response.status !== 200) {

      throw new Error("Error while reading file.");

    }

    let text = await response.text();



    var res = text.split([3], "\n");

    for (let i = 0; i <= res.length; i++) {


    }

    displayArea.innerHTML = res


  } catch (err) {

    displayArea.innerHTML = "Unknown Error " + err;

  } finally {

    loader.style.display = "none";
  }


}
 

Solution

async function getData(){
  
  let response = await fetch(`https://v-dresevic.github.io/Advanced-JavaScript-Programming/data/students.txt`);

    if (response.status !== 200) {
      throw new Error("Error while reading file.");
    }

    let text = await response.text();
    let arr = text.split("\n");
    let result = arr.reduce((resultArray, item, index) => { 
      const chunkIndex = Math.floor(index/4);
      if(!resultArray[chunkIndex]) {
        resultArray[chunkIndex] = []
      }
      resultArray[chunkIndex].push(item.replace(/(.+)(\r)$/, '$1'));
      return resultArray
    }, []).map(d=>{
      return {Name: d[0], Address: d[1], Phone: d[2], Course: d[3]};
    });  

    return result;
}

async function func(){

let result = await getData();
console.log(result);

}

func();

Answered By – akicsike

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published