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

327
Views
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 answers
Answer question

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 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!