Implement a function to parse a CSV string into an array of objects.
HardProblem Statement
Implement a function to parse a CSV string into an array of objects.
Sample Input
"Name,Age,City\nJohn,25,New York\nAlice,30,Los Angeles"
Sample Output
[ { Name: 'John', Age: '25', City: 'New York' },
{ Name: 'Alice', Age: '30', City: 'Los Angeles' } ]
Limits
Time: 2s · Memory: 256 MB
Code Editor
00:00
// Parse a CSV string into an array of objects
function parseCSV(csv) {
// Split CSV by newline to get rows
// Use the first row as keys for the objects
// Map remaining rows to objects with keys and values
}
console.log(parseCSV("Name,Age,City\nJohn,25,New York\nAlice,30,Los Angeles"));
Input
"Name,Age,City\nJohn,25,New York\nAlice,30,Los Angeles"
Expected Output
[ { Name: 'John', Age: '25', City: 'New York' },
{ Name: 'Alice', Age: '30', City: 'Los Angeles' } ]