Published
Edited
Aug 18, 2020
1 fork
4 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data.columns
Insert cell
Insert cell
Insert cell
data.forEach(currentElement => {
if (currentElement.rating >= 75) {
currentElement.grade = 'A';
} else if (currentElement.rating >= 50) {
currentElement.grade = 'B';
} else if (currentElement.rating >= 25) {
currentElement.grade = 'C';
} else {
currentElement.grade = 'D';
}
})
Insert cell
Insert cell
Insert cell
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Insert cell
Insert cell
evens = nums.filter(currentElement => currentElement % 2 === 0)
Insert cell
Insert cell
Insert cell
data.filter(currentElement => currentElement.type === 'H')
Insert cell
Insert cell
data.filter(currentElement => currentElement.calories < 100 && currentElement.sodium === 0)
Insert cell
Insert cell
Insert cell
nums
Insert cell
Insert cell
nums.map(currentElement => currentElement * 2)
Insert cell
Insert cell
words = ['I', 'love', 'information', 'visualization']
Insert cell
wordLengths = words.map(currentElement => currentElement.length)
Insert cell
Insert cell
Insert cell
Insert cell
cleanedData = data.map(currentElement => ({
'name': currentElement.name,
'rating': currentElement.rating,
'type': currentElement.type === "H" ? "hot" : "cold",
'brand': abbreviationToBrandName[currentElement.mfr]
}))
Insert cell
Insert cell
Insert cell
nums
Insert cell
nums.reduce((total, currentValue) => {
total += currentValue;
return total;
}, 0) // This 0 is the initial value for total. See what happens when you change it!
Insert cell
Insert cell
cleanedData.reduce((accumulator, currentValue) => {
/*
Get the count of cereals that we have seen for this brand so far.
If this is the first time we are seeing this brand, then
accumulator[currentValue.brand] will return undefined, which evaluates
to false, so count is given the value of the right side of the
or operator (||), which is 0.
*/
const count = accumulator[currentValue.brand] || 0;
// update the count for this brand
accumulator[currentValue.brand] = count + 1;
return accumulator;
}, {}); // an empty object, {}, is our initial value
Insert cell
Insert cell
cleanedData.reduce((accumulator, currentValue) => {
let count = 0;
// hasOwnProperty will check if there's a key for this brand in the accumulator object
if (accumulator.hasOwnProperty(currentValue.brand)) {
count = accumulator[currentValue.brand];
}
accumulator[currentValue.brand] = count + 1;
return accumulator;
}, {});
Insert cell
Insert cell
Insert cell
Insert cell
unsortedNums = [9, 4, 5, 7, 2, 1, 10, 3, 8, 6]
Insert cell
// If you remove the call to slice and rerun, notice what happens when you run the cell below
unsortedNums.slice().sort(d3.ascending)
Insert cell
unsortedNums
Insert cell
unsortedNums.slice().sort(d3.descending)
Insert cell
Insert cell
cleanedData.slice()
.sort((a, b) => d3.descending(a.rating, b.rating))
.slice(0, 5) // get elements starting with index 0 and stopping before index 5
Insert cell
Insert cell
Insert cell
d3.min(data, d => d.rating)
Insert cell
Insert cell
d3.max(data, d => d.rating)
Insert cell
Insert cell
d3.extent(data, d => d.rating)
Insert cell
Insert cell
d3.sum(data, d => d.protein)
Insert cell
Insert cell
d3.median(data, d=> d.calories)
Insert cell
Insert cell
d3.mean(data, d => d.calories)
Insert cell
Insert cell
d3 = require("d3@5", "d3-array@2")
Insert cell
Insert cell
brandToCereals = d3.group(cleanedData, d => d.brand)
Insert cell
Insert cell
Insert cell
d3.rollup(cleanedData, v => v, d => d.brand)
Insert cell
Insert cell
/*
The first argument is our data
The second argument is the function that gets the value for each group,
where v is an array of objects for each group
The third argument is the function to say what key to group by
*/
d3.rollup(cleanedData, v => v.length, d => d.brand)
Insert cell
Insert cell
d3.rollup(cleanedData,
arr => d3.mean(arr, cereal => cereal.rating),
d => d.brand)
Insert cell
Insert cell
d3.rollup(cleanedData, arr => ({
minRating: d3.min(arr, cereal => cereal.rating),
maxRating: d3.max(arr, cereal => cereal.rating)
}), d => d.brand)
Insert cell
Insert cell
brandToMedianRating = d3.rollup(cleanedData,
arr => d3.median(arr, cereal => cereal.rating),
d => d.brand)
Insert cell
Insert cell
brandAndMedianRating = Array.from(brandToMedianRating, ([brand, median]) => ({brand, median}))
Insert cell
Insert cell
topBrandsByMedianRating = brandAndMedianRating.slice()
.sort((a, b) => d3.descending(a.median, b.median))
.slice(0, 3)
Insert cell
Insert cell
topBrandsByMedianRating.map(d => d.brand)
Insert cell
Insert cell
{
const brandToMedian = d3.rollup(cleanedData,
arr => d3.median(arr, cereal => cereal.rating),
d => d.brand)

return Array.from(brandToMedian, ([brand, median]) => ({brand, median}))
.sort((a, b) => d3.descending(a.median, b.median))
.slice(0, 3)
.map(d => d.brand)
}
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