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?
>Solution :
You’re splitting by the newline. The newline literal is \n, (your slash is the wrong way)