Skip to content

Update README.md - example for map/reduce with nested properties. #182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,35 @@ const index = new Supercluster({

Note that `reduce` must not mutate the second argument (`props`).

If `reduce` accumulates a nested property, the property of `accumulator` must cloned before being mutated and assigned back to `accumulator`. This avoids unexpected results of passing the object by reference.

Or, avoid nesting data to be accumulated.

Example collecting categorical information about a cluster:

```js
const index = new Supercluster({
map: (props) => ({ categories: { [props.myCategory]: 1} }), // ex. { categories: { tall: 1 } }
reduce: (accumulated, props) => {
const categories = {};
// clone the categories object from the accumulator
for (const key in accumulated.categories) {
categories[key] = accumulated.categories[key];
}
// add props' category data to the clone
for (const key in props.categories) {
if (key in accumulated.categories) {
categories[key] = accumulated.categories[key] + props.categories[key];
} else {
categories[key] = props.categories[key];
}
}
// assign the clone to the accumulator
accumulated.categories = categories; // ex. { categories: { tall: 1, medium: 1 } }
}
})
```

## Developing Supercluster

```
Expand Down