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

328
Visualizações
Fetch - is there an easier way to convert this to an array or a readable format where I can loop through it?

If I use 'application/text' and res.text(), it returns plain text and not an array. If I use res.json(), I get an invalid token at the end '<' If I JSON.stringify(final), I get a string of extra new line characters that are not parseable. Is there another way to ready this from a public folder?

Thanks

This is the sample file structure I was given, not a json but an array of object entities:

filename: newFile.dto

[
  {
    id: 0,
    img: 'C:\\ReactProjects\\IMAGES\\DSC_0134.jpg',
    test: 'another column'
  },
  {
    id: 1,
    img: 'C:\\ReactProjects\\IMAGES\\DSC_0135.jpg',
    test: 'another column 1'
  },
  {
    id: 2,
    img: 'C:\\ReactProjects\\IMAGES\\DSC_0136.jpg',
    test: 'another column 2'
  },
  {
    id: 3,
    img: 'C:\\ReactProjects\\IMAGES\\DSC_0137.jpg',
    test: 'another column 3'
  }
]


  async function testFileRead()
  {
     let myArray = await fetch('newFile.dto',{
        headers : { 
          'Content-Type': 'application/text',
          'Accept': 'application/text'
         }
       }
      )
      .then(res =>{
        return res.text(); //res.json()
      })
      .then(final =>{
        return final;
      })
  }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You could use eval() after fetch:

  console.log("data", myArray); //text as string
  console.log("eval", eval(myArray)); //array JS

enter image description here Note Never use eval()!: Executing JavaScript from a string is an enormous security risk.

As Keith suggest use at leas is safer than eval:

  const arr = new Function("return [..." + myArray + "]")();
  console.log(arr.map((obj) => obj.test));// ['another column', 'another column 1', 'another column 2', 'another column 3']
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