Update nest.md

This commit is contained in:
Angelos Chalaris
2018-02-26 12:23:46 +02:00
committed by GitHub
parent 7a457af1f9
commit b81993ef81

View File

@ -3,12 +3,10 @@
Given a flat array of objects linked to one another, it will nest them recursively. 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. 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`, Use recursion.
then use `Array.map()` to map each one to a new object that has a `children` property which Use `Array.filter()` to filter the items where the `id` matches the `link`, then `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 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 object).
argument, `id`, to default to `null` which indicates the object is not linked to another one (i.e., Omit the third argument, `link`, to use `'parent_id'` as the default property which links the object to another one by its `id`.
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 ```js
const nest = (items, id = null, link = 'parent_id') => const nest = (items, id = null, link = 'parent_id') =>