Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

209
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda