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

267
Views
Error in assigning elements in array in JS

i am trying to store list data into an object received from axios response. the first set of data being headers i am storing in cols as array and the rest in rows. The data received is all defined and after parsing it perfectly logged in the console ie all defined. inside the loop when loading of rows elements starts, the first set of elements gets stored but for the next set ie for the value of i = 2 I am getting error saying cannot set property of undefined (setting 0).

For convenience I have changed the type of data received from the axios

        let response = {data:'"A","B"\n"C","D"\n"E","F"'} //await axios(URL)
        let raw = response.data.split(/\r?\n/);
        let data = {
            cols:[],
            rows:[]   // I have tried rows:[[]] or rows:[{v:[]}]
        }

        for (let i in raw) {
            raw[i] = raw[i].split(",");
            for(let j in raw[i]){
                raw[i][j] = raw[i][j].replace(/"/g, '')
                if (i==0)
                    data.cols[j]=raw[i][j]
                else{
                    data.rows[i-1][j]=raw[i][j] // for rows as object => data.rows[i-1].v[j]
                    //console.log(i+'->'+data.rows[i-1])
                }
            }        
        }
        return data // this is not matter of concern
    }   

I have tried declaring the row array as 2D array but error persists. hoving the mouse over the object gives rows (property) : never[] and same with cols.

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

0

You can do something like this

let response = {data:'"A","B"\n"C","D"\n"E","F"'}

const [cols, ...rows] = response.data.split('\n').map(r => r.replace(/"/g, '').split(','))

const data = {
  cols,
  rows
}

console.log(data)

about 4 years ago · Juan Pablo Isaza Report

0

You can do:

const response = { data: '"A","B"\n"C","D"\n"E","F"' }
const [cols, ...rows] = response.data.split(/\r?\n/).map(r => r.match(/[A-Z]+/g))
const data = { cols, rows }

console.log(data)

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!