Published
Edited
Oct 16, 2021
2 stars
Insert cell
Insert cell
Insert cell
mutable allUsers = ({
100000: {
id: 100000,
email: 'foo@bar.com'
},
100001: {
id: 100001,
email: 'bar@foo.fi'
}
})
Insert cell
Insert cell
Insert cell
page1 = fetchUsers(1) // First page has the first six users
Insert cell
Insert cell
viewof button = Inputs.button("Fetch page 2", { reduce () { fetchUsers(2) } })
Insert cell
page3 = fetchUsers(3) // Third page is empty, all good
Insert cell
Insert cell
Insert cell
fetchedUserCount = Object.keys(allUsers).length
Insert cell
usersByEmailDomain = {
return lodash.groupBy(allUsers, (user) => {
return user.email.substr(user.email.indexOf('@') + 1)
})
}
Insert cell
Insert cell
Insert cell
async function fetchUsers(page) {
// Fetch users on a particular page
const response = await axios.get(
`https://reqres.in/api/users?page=${page || 1}`
);

// Next, I want to ADD all the new users to `allUsers` object by ID
const responseUsers = response.data.data;
const responseUsersById = lodash.keyBy(responseUsers, "id");

// Note the use of the `mutable` keyword here
mutable allUsers = { ...mutable allUsers, ...responseUsersById };

// Return the IDs for this page
return responseUsers.map(({ id }) => {
return id;
});
}
Insert cell
Insert cell
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