Published
Edited
Aug 31, 2020
1 star
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
{
let count = 0;
data.forEach(function(d) {
count += 1;
});
return count;
}
Insert cell
md`Also consider the [for...of](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of) alternative that might be for legible`
Insert cell
{
let count = 0;
for (let d of data) {
count += 1;
}
return count;
}
Insert cell
Insert cell
data.length
Insert cell
Insert cell
Insert cell
Insert cell
copyOfData = {
let c = { ...dataObject };
c.age = +c.age;
c.salary = +c.salary;
return c;
}
Insert cell
Insert cell
dataObject2 = ({ name: "Saul", stats: { age: "55" }, height: "6.6" })
Insert cell
shallowCopy = {
let c = { ...dataObject2 };
// let c = Object.assign({}, dataObject2); //Another option

c.height = +c.height;
c.stats.age = +c.stats.age;
return c;
}
Insert cell
Insert cell
Insert cell
dataObject3 = ({ name: "Saul", stats: { age: "55" } })
Insert cell
deepCopy = {
let c = JSON.parse(JSON.stringify(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});
return a;
}
Insert cell
Insert cell
Insert cell
smallData = data.map((d, i) => ({
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
sortedData = {
let dataCopy = data.map(d => ({ ...d })); // Make a clone of the data
dataCopy.sort((a, b) => b.population - a.population);
return dataCopy;
}
Insert cell
Insert cell
Insert cell
populations.sort(d3.descending)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
landSum = data.reduce((sum, d) => sum + d.land_area, 0)
Insert cell
Insert cell
weirdString = data.reduce((str, d, i) => {
const 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

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