El siguiente código es para csv a matriz. Obtengo un "espacio" adicional en la salida.
Ejemplo:
salida: {lat: 42.546245, "lng": 1.601554}
esperado: {lat: 42.546245, "lng": 1.601554} Se necesita ayuda para eliminar el espacio adicional en el encabezado "lng".
function initMap() { mapLoad(); readFiles(); } function readFiles() { const myForm = document.getElementById("myForm"); const csvFile = document.getElementById("csvFile"); function csvToArray(str, delimiter = ",") { // slice from start of text to the first \n index // use split to create an array from string by delimiter console.log(str); const headers = str.slice(0, str.indexOf("\n")).split(delimiter); // slice from \n index + 1 to the end of the text // use split to create an array of each csv value row // const rows = str.slice(str.indexOf("\n") + 1).split("\n"); const rows = str.slice(str.indexOf("\n") + 1).split("\n"); // Map the rows // split values from each row into an array // use headers.reduce to create an object // object properties derived from headers:values // the object passed as an element of the array const arr = rows.map(function (row) { const values = row.split(delimiter).map(Number); const el = headers.reduce(function (object, header, index) { object[header] = values[index]; return object; }, {}); return el; }); // return the array return arr; } myForm.addEventListener("submit", function (e) { e.preventDefault(); const input = csvFile.files[0]; const reader = new FileReader(); reader.onload = function (e) { const text = e.target.result; const data = csvToArray(text); console.log(data); mapLoad_1(data); }; reader.readAsText(input); }); }puede usar la función de recorte en el encabezado
object[header.trim()] = values[index];Cuando obtenga el título, puede eliminar todos los espacios a través de la función trim ()
const headers = str.slice(0,str.indexOf("\n")).trim().split(delimiter); const rows = str.slice(str.indexOf("\n") + 1).trim().split("\n");