Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

CSV to JSON parsing creating an empty array

I’m very new to javascript (this is my first project) and I’m currently trying to parse a CSV file to a JSON. My current code looks like this:

document.getElementById("Import").onclick = async function importData() {
    const responce = await fetch('Books.csv') 
    const data = await responce.text()
    const rows = data.split('/n').slice(1)
    console.log(data)
    console.log(rows)
    for (let i = 0; i <= rows.length; i++){
        columns = rows[i].split(',').slice(0,3)
    }
    console.log(columns)
        for (let i = 0; i <= (columns.length)/4; i++){
            inventory[i] = {
                bookCode: columns(4*i),
                bookName: columns(4*i + 1),
                copies: columns(4*i + 2),
                borrowed: columns(4*i + 3)          
            }
        }
    
}

However the console shows rows as being an empty array. Could anyone please tell my why this is happening and how to fix it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You’re splitting by the newline. The newline literal is \n, (your slash is the wrong way)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading