Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
JavaScript Object - Split() method + '\n'

I reached 'fetch' method...

A simple question (complicated for me) ... How to use 'fetch()' method, 'split()' and '\ n' together?

I will show you an example (i am here to learn and master some skills and i am not ashamed to ask):

I need to read and print the following data using the 'fetch' method:

from the following link 'https://v-dresevic.github.io/Advanced-JavaScript-Programming/data/students.txt' - it is necessary to read the data and print them on the page.

And that is quite clear to me! I managed to do that!

code: enter image description here

my result (wrong result): enter image description here

correct result: enter image description here

My question is: After reading the data from the file, I have to parse them and create Student objects based on them. Parsing can be done using the split () method of the String object. It is best to divide the read text by line breaks, specifying "\ n" for the split () method parameter.

thanks in advance :)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

here is a fast example of parsing your data to an array of objects as i think this is the only thing you ask here, from there you can loop that array and display the object as you need.

const url =
  "https://v-dresevic.github.io/Advanced-JavaScript-Programming/data/students.txt";
let result = fetch(url)
  .then((r) => r.text())
  .then(process);
function process(result) {
  const linesDescription = ["Name", "Address", "Phone", "Course"];
  const array = [];
  let obj = {};
  var lines = result.split("\n");
  let x = 0;
  for(var line = 0; line < lines.length; line++){
    obj[linesDescription[x]] = lines[line].trim();
    x++;
    if (x >= linesDescription.length) {
      array.push(obj);
      x = 0;
      obj = {};
    }
  };
  console.log(array);
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!