Published
Edited
Dec 8, 2019
Insert cell
Insert cell
data = [
{"city":"seattle", "state":"WA", "population":652405, "land_area":83.9},
{"city":"new york", "state":"NY", "population":8405837, "land_area":302.6},
{"city":"boston", "state":"MA", "population":645966, "land_area":48.3},
{"city":"kansas city", "state":"MO", "population":467007, "land_area":315}
]
Insert cell
Insert cell
iterate_forEach = {
let count = 0;
data.forEach(function(d) {
count += 1;
});
return count;
}
Insert cell
Insert cell
data.length
Insert cell
Insert cell
Insert cell
copyOfData = {
let c = _.clone(dataObject);
c.age = +c.age;
c.salary = +c.salary;
return c;
}
Insert cell
Insert cell
Insert cell
shallowCopy = {
let c = _.clone(dataObject2);
c.stats.age = +c.stats.age; // updated by reference
return c;
}
Insert cell
Insert cell
Insert cell
Insert cell
deepCopy = {
let c = _.cloneDeep(dataObject3);
c.stats.age = +c.stats.age;
return c;
}
Insert cell
Insert cell
Insert cell
Insert cell
spreadCopy = {
let a = [...dataArray];
a.push({name: "Mary", age: 34});
a[0].age = 60; // shared reference not copied
return a;
}
Insert cell
Insert cell
Insert cell
data
Insert cell
smallData = data.map(function(d,i) {
return {
name: d.city.toUpperCase(),
index: i + 1,
rounded_area: Math.round(d.land_area)
};
})
Insert cell
Insert cell
Insert cell
large_land = data.filter(d => d.land_area > 200)
Insert cell
Insert cell
data
Insert cell
sortedData = {
let d = [...data];
d.sort(function(a,b) {
return b.population - a.population; // if positive, b goes before a
});
return d;
}
Insert cell
Insert cell
populations = data.map(d => d.population)
Insert cell
populations.sort(d3.descending)
Insert cell
Insert cell
Insert cell
Insert cell
_.clone(nums).sort()
Insert cell
Insert cell
landSum = data.reduce(function(sum, d, i, data) {
return sum + d.land_area;
}, 0)
Insert cell
Insert cell
weirdString = data.reduce(function(str, d, i, data) {
var ending = (i % 2 === 0) ? " is cool." : " sucks." ;
return str + " " + d.city + ending;
}, "")
Insert cell
Insert cell
bigCities = data.filter(d => d.population > 500000)
.sort((a,b) => a.population - b.population)
.map(d => d.city)
Insert cell
Insert cell
Insert cell
Insert cell
stylesheet
Insert cell
import { stylesheet } from "@embracelife/tutorial-utilities"
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