Modo entrevista. Temporizador de 6 min, sin pistas y sin autocompletado
CSV to objects
FácilJSON and data6 min
Test data came as a CSV string. Lines are separated by \n, values in a line are separated by commas, and the first line holds the headers. Write a function csvToObjects(csv) that returns an array of objects, one for every data line. The keys come from the headers, and the values stay strings.
Ejemplos
csvToObjects('name,age\nAnn,30\nBob,25')Devuelve [ { name: 'Ann', age: '30' }, { name: 'Bob', age: '25' } ]
csvToObjects('id,title')Devuelve []
Preguntada en Infosys · Fuente
Cargando el editor…
- Entrada
csvToObjects('name,age\nAnn,30\nBob,25')Esperado[ { name: 'Ann', age: '30' }, { name: 'Bob', age: '25' } ] - Entrada
csvToObjects('login,role,active\nadmin,owner,true')Esperado[ { login: 'admin', role: 'owner', active: 'true' } ] - Entrada
csvToObjects('id,title')Esperado[]
Las pruebas ocultas se ejecutan al enviar