beautifulpython – 17-16 Aug 2

greetings, given any csv data how can one get an array dynamically mapped to key(column_name) value(row_value)? example csv
name,age,salary
jo,32,40000
jane,27,50000
name,age,salary
jo,32,40000
jane,27,50000
result
[
{name: jo, age: 32, salary: 40000},
{name: jane, age: 27, salary: 27,50000}
]
[
{name: jo, age: 32, salary: 40000},
{name: jane, age: 27, salary: 27,50000}
]
Gracias!
B
beautifulpython260d ago
d3.csv does this automagically.
UU
Unknown User260d ago
B
beautifulpython260d ago
diving in step 1
let url = 'https://raw.githubusercontent.com/plotly/datasets/master/3d-scatter.csv';

(async () => {
let data = await fetch(url);
let response = await data.text();
console.log('response', response);
return response;
})();
let url = 'https://raw.githubusercontent.com/plotly/datasets/master/3d-scatter.csv';

(async () => {
let data = await fetch(url);
let response = await data.text();
console.log('response', response);
return response;
})();
found this. will clean it up and voila
const CSVToJSON = (data, delimiter = ',') => {
const titles = data.slice(0, data.indexOf('\n')).split(delimiter);
return data
.slice(data.indexOf('\n') + 1)
.split('\n')
.map(v => {
const values = v.split(delimiter);
return titles.reduce(
(obj, title, index) => ((obj[title] = values[index]), obj),
{}
);
});
};
const CSVToJSON = (data, delimiter = ',') => {
const titles = data.slice(0, data.indexOf('\n')).split(delimiter);
return data
.slice(data.indexOf('\n') + 1)
.split('\n')
.map(v => {
const values = v.split(delimiter);
return titles.reduce(
(obj, title, index) => ((obj[title] = values[index]), obj),
{}
);
});
};
UU
Unknown User258d ago