Modo entrevista. Cronômetro de 6 min, sem dicas e sem autocompletar
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.
Exemplos
csvToObjects('name,age\nAnn,30\nBob,25')Retorna [ { name: 'Ann', age: '30' }, { name: 'Bob', age: '25' } ]
csvToObjects('id,title')Retorna []
Cobrada em Infosys · Fonte
Carregando o 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[]
Os testes ocultos rodam no envio