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!
4 Replies
beautifulpython
beautifulpythonOP2y ago
d3.csv does this automagically.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
beautifulpython
beautifulpythonOP2y 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),
{}
);
});
};
reactibot
reactibot2y ago
This thread hasn’t had any activity in 36 hours, so it’s now locked. Threads are closed automatically after 36 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1136346738131734609