From a626e56b21850ac1b412e38ef303a51c863f56f3 Mon Sep 17 00:00:00 2001 From: emma bishop Date: Wed, 21 Jul 2021 10:12:19 -0400 Subject: [PATCH] Update README.md - example for map/reduce with nested properties. --- README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/README.md b/README.md index 61aa6245..ac53d4a8 100644 --- a/README.md +++ b/README.md @@ -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 ```