Travis build: 1734
This commit is contained in:
41
README.md
41
README.md
@ -344,6 +344,7 @@ average(1, 2, 3);
|
|||||||
* [`matches`](#matches)
|
* [`matches`](#matches)
|
||||||
* [`matchesWith`](#matcheswith)
|
* [`matchesWith`](#matcheswith)
|
||||||
* [`merge`](#merge)
|
* [`merge`](#merge)
|
||||||
|
* [`nest`](#nest)
|
||||||
* [`objectFromPairs`](#objectfrompairs)
|
* [`objectFromPairs`](#objectfrompairs)
|
||||||
* [`objectToPairs`](#objecttopairs)
|
* [`objectToPairs`](#objecttopairs)
|
||||||
* [`omit`](#omit)
|
* [`omit`](#omit)
|
||||||
@ -6259,6 +6260,46 @@ merge(object, other); // { a: [ { x: 2 }, { y: 4 }, { z: 3 } ], b: [ 1, 2, 3 ],
|
|||||||
<br>[⬆ Back to top](#table-of-contents)
|
<br>[⬆ Back to top](#table-of-contents)
|
||||||
|
|
||||||
|
|
||||||
|
### 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. 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') =>
|
||||||
|
items
|
||||||
|
.filter(item => item[link] === id)
|
||||||
|
.map(item => ({ ...item, children: nest(items, item.id) }));
|
||||||
|
```
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>Examples</summary>
|
||||||
|
|
||||||
|
```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: [...] }]
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
<br>[⬆ Back to top](#table-of-contents)
|
||||||
|
|
||||||
|
|
||||||
### objectFromPairs
|
### objectFromPairs
|
||||||
|
|
||||||
Creates an object from the given key-value pairs.
|
Creates an object from the given key-value pairs.
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@ -12,7 +12,9 @@ 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') =>
|
||||||
items.filter(item => item[link] === id).map(item => ({ ...item, children: nest(items, item.id) }))
|
items
|
||||||
|
.filter(item => item[link] === id)
|
||||||
|
.map(item => ({ ...item, children: nest(items, item.id) }));
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
@ -23,7 +25,7 @@ const comments = [
|
|||||||
{ id: 3, parent_id: 1 },
|
{ id: 3, parent_id: 1 },
|
||||||
{ id: 4, parent_id: 2 },
|
{ id: 4, parent_id: 2 },
|
||||||
{ id: 5, parent_id: 4 }
|
{ id: 5, parent_id: 4 }
|
||||||
]
|
];
|
||||||
const nestedComments = nest(comments) // [{ id: 1, parent_id: null, children: [...] }]
|
const nestedComments = nest(comments); // [{ id: 1, parent_id: null, children: [...] }]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user