1.2 KiB
1.2 KiB
title, type, language, tags, cover, dateModified
| title | type | language | tags | cover | dateModified | ||
|---|---|---|---|---|---|---|---|
| Nest objects | snippet | javascript |
|
birds | 2020-10-21T21:54:53+03:00 |
Nests recursively objects linked to one another in a flat array.
- Use recursion.
- Use
Array.prototype.filter()to filter the items where theidmatches thelink. - Use
Array.prototype.map()to map each item to a new object that has achildrenproperty which recursively nests the items based on which ones are children of the current item. - Omit the second argument,
id, to default tonullwhich indicates the object is not linked to another one (i.e. it is a top level object). - Omit the third argument,
link, to use'parent_id'as the default property which links the object to another one by itsid.
const nest = (items, id = null, link = 'parent_id') =>
items
.filter(item => item[link] === id)
.map(item => ({ ...item, children: nest(items, item.id, link) }));
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: [...] }]