Published
Edited
Sep 16, 2019
1 fork
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
csv = await fetch('https://raw.githubusercontent.com/anbnyc/major-studio-1-fa19/master/lab3_p5_csv/met_objects_csv/data/MetObjects-sorted-1970-2017.csv', {method: "GET"}).then(data=>data.text());
Insert cell
Insert cell
typeof csv
Insert cell
Insert cell
headers = csv.split('\n')[0].split(',');
Insert cell
Insert cell
rows = csv.split('\n').slice(1,-1).map(row=>row.split(','));
Insert cell
csvObject = rows.map(row => {
let myObject = {};
// Using our "headers" as a proxy for the number of columns
// we will use a .forEach statement to associate row data
// with the header at the corresponding position
headers.forEach((elem, index)=>{
// myObject["Object ID"] = row[0]
myObject[elem] = row[index];
});
// This can be more effecient than using a
// hard coded "for loop" because it will work
// no matter which CSV is provided.
return myObject;
});
Insert cell
Insert cell
Insert cell
coats = csvObject.filter( item => {
// The .filter method will only keep
// data that matches the following conditional logic
// If Object Name is Equal to Coat and
// Object Begin Date is Equal to 1970
// then keep this Object.
return item["Object Name"] == 'Coat' &&
item["Object Begin Date"] == '1970'
});
Insert cell
dress = csvObject.filter( item => {
// Using "Includes" is better than
// using a hard coded string comparison, because
// if our title is "Cocktail Dress" for instane,
// then it will not be removed from our dataset
return item["Object Name"].includes('Dress');
// Compare this to the output below.
});
Insert cell
dressStrict = csvObject.filter( item => {
return item["Object Name"] == 'Dress';
});
Insert cell
Insert cell
Insert cell
Insert cell
uniqueNames = {
let unique = {};

csvObject.forEach( item => {
if( !unique[item["Object Name"]] ){
unique[item["Object Name"]] = 1;
} else {
unique[item["Object Name"]] += 1;
}
});
return unique;
}
Insert cell
Object.keys(uniqueNames).length
Insert cell
Insert cell
sortedUniqueNames = {
// this uses a "selection sorting" algorithm
// reference: https://en.wikipedia.org/wiki/Selection_sort
let usedValues = [];
let sorted = [];
while ( !(sorted.length >= Object.keys(uniqueNames).length)){
let max = { "name": '', "value": '' };
for (let key in uniqueNames){
if (
uniqueNames[key] > max.value &&
!usedValues.includes(key)
){
max.name = key;
max.value = uniqueNames[key];
usedValues.push(key);
}
}
sorted.push(max);
}
return sorted;
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more