Fix a bug with nest snippet

Pass `link` to nested `nest` call.
Resolves #1116.
This commit is contained in:
Angelos Chalaris
2020-04-15 20:23:45 +03:00
committed by GitHub
parent 66fbafd0db
commit d85785b540

View File

@ -15,7 +15,7 @@ Omit the third argument, `link`, to use `'parent_id'` as the default property wh
const nest = (items, id = null, link = 'parent_id') =>
items
.filter(item => item[link] === id)
.map(item => ({ ...item, children: nest(items, item.id) }));
.map(item => ({ ...item, children: nest(items, item.id, link) }));
```
```js
@ -28,4 +28,4 @@ const comments = [
{ id: 5, parent_id: 4 }
];
const nestedComments = nest(comments); // [{ id: 1, parent_id: null, children: [...] }]
```
```