From f6ced04f613e4a5a3c877c852b54b8a758a3023f Mon Sep 17 00:00:00 2001 From: atomiks Date: Sat, 24 Feb 2018 22:49:09 +1100 Subject: [PATCH 1/4] Create nest --- snippets/nest | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 snippets/nest diff --git a/snippets/nest b/snippets/nest new file mode 100644 index 000000000..9bd3bbf91 --- /dev/null +++ b/snippets/nest @@ -0,0 +1,26 @@ +### nest + +Given a flat array of objects linked to one another, it will nest them recursively. +Useful for nesting comments, such as the ones on reddit.com. + +Use recursion. Use `Array.filter()` to filter the items where the `id` matches the `link`, +then use `Array.map()` to map each one to a new object that has a `children` property which +recursively nests the items based on which ones are children of the current item. + +```js +const nest = (items, id = null, link = 'parent_id') => + items.filter(item => item[link] === id).map(item => ({ ...item, children: nest(items, item.id) })) +``` + +```js +// One top level comment +const comments = [ + { id: 1, parent_id: null }, + { id: 2, parent_id: 1 }, + { id: 3, parent_id: 1 }, + { id: 4, parent_id: 2 }, + { id: 5, parent_id: 4 } +] +const nestedComments = nest(comments) // [{ id: 1, parent_id: null, children: [...] }] +``` + From cae90185f2043c004f60a3f9bd2eb578eb889465 Mon Sep 17 00:00:00 2001 From: atomiks Date: Sat, 24 Feb 2018 22:49:56 +1100 Subject: [PATCH 2/4] Rename nest to nest.md --- snippets/{nest => nest.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename snippets/{nest => nest.md} (100%) diff --git a/snippets/nest b/snippets/nest.md similarity index 100% rename from snippets/nest rename to snippets/nest.md From fd10f180843636fac6c478eda078894ddce5aed3 Mon Sep 17 00:00:00 2001 From: atomiks Date: Sat, 24 Feb 2018 22:51:42 +1100 Subject: [PATCH 3/4] Update nest.md --- snippets/nest.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/snippets/nest.md b/snippets/nest.md index 9bd3bbf91..40eb911c7 100644 --- a/snippets/nest.md +++ b/snippets/nest.md @@ -5,7 +5,10 @@ Useful for nesting comments, such as the ones on reddit.com. Use recursion. Use `Array.filter()` to filter the items where the `id` matches the `link`, then use `Array.map()` to map each one to a new object that has a `children` property which -recursively nests the items based on which ones are children of the current item. +recursively nests the items based on which ones are children of the current item. Omit the second +argument, `id`, to default to `null` which indicates the object is not linked to another one (i.e., +it is a top level). Omit the third argument, `link`, to use `'parent_id'` as the default property +which links the object to another one by its `id`. ```js const nest = (items, id = null, link = 'parent_id') => From 1c98113190d1c31aab1cf12455931c4a37a4d288 Mon Sep 17 00:00:00 2001 From: atomiks Date: Sat, 24 Feb 2018 22:52:39 +1100 Subject: [PATCH 4/4] Update tag_database --- tag_database | 1 + 1 file changed, 1 insertion(+) diff --git a/tag_database b/tag_database index 54db11193..8d8f55279 100644 --- a/tag_database +++ b/tag_database @@ -162,6 +162,7 @@ minBy:math,array,function minN:array,math mostPerformant:utility,function negate:function +nest:object none:array,function nthArg:utility,function nthElement:array