R
Reactiflux

beautifulpython – 17-16 Aug 2

beautifulpython – 17-16 Aug 2

Bbeautifulpython8/2/2023
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! d3.csv does this automagically.
UUUnknown User8/2/2023
Message Not Public
Sign In & Join Server To View
Bbeautifulpython8/2/2023
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),
{}
);
});
};
UUUnknown User8/4/2023
Message Not Public
Sign In & Join Server To View

Looking for more? Join the community!

R
Reactiflux

beautifulpython – 17-16 Aug 2

Join Server