Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

222
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda