Travis build: 1740
This commit is contained in:
10
README.md
10
README.md
@ -6267,12 +6267,10 @@ merge(object, other); // { a: [ { x: 2 }, { y: 4 }, { z: 3 } ], b: [ 1, 2, 3 ],
|
||||
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`.
|
||||
Use recursion.
|
||||
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.
|
||||
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).
|
||||
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') =>
|
||||
|
||||
@ -1478,7 +1478,7 @@ Foo<span class="token punctuation">.</span>prototype<span class="token punctuati
|
||||
c<span class="token punctuation">:</span> <span class="token string">'foo'</span>
|
||||
<span class="token punctuation">};</span>
|
||||
<span class="token function">merge</span><span class="token punctuation">(</span>object<span class="token punctuation">,</span> other<span class="token punctuation">);</span> <span class="token comment">// { a: [ { x: 2 }, { y: 4 }, { z: 3 } ], b: [ 1, 2, 3 ], c: 'foo' }</span>
|
||||
</pre><button class="primary clipboard-copy">📋 Copy to clipboard</button></div></div><div class="card fluid"><h3 id="nest" class="section double-padded">nest</h3><div class="section double-padded"><p>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.</p><p>Use recursion. Use <code>Array.filter()</code> to filter the items where the <code>id</code> matches the <code>link</code>, then use <code>Array.map()</code> to map each one to a new object that has a <code>children</code> property which recursively nests the items based on which ones are children of the current item. Omit the second argument, <code>id</code>, to default to <code>null</code> which indicates the object is not linked to another one (i.e., it is a top level). Omit the third argument, <code>link</code>, to use <code>'parent_id'</code> as the default property which links the object to another one by its <code>id</code>.</p><pre class="language-js"><span class="token keyword">const</span> nest <span class="token operator">=</span> <span class="token punctuation">(</span>items<span class="token punctuation">,</span> id <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">,</span> link <span class="token operator">=</span> <span class="token string">'parent_id'</span><span class="token punctuation">)</span> <span class="token operator">=></span>
|
||||
</pre><button class="primary clipboard-copy">📋 Copy to clipboard</button></div></div><div class="card fluid"><h3 id="nest" class="section double-padded">nest</h3><div class="section double-padded"><p>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.</p><p>Use recursion. Use <code>Array.filter()</code> to filter the items where the <code>id</code> matches the <code>link</code>, then <code>Array.map()</code> to map each one to a new object that has a <code>children</code> property which recursively nests the items based on which ones are children of the current item. Omit the second argument, <code>id</code>, to default to <code>null</code> which indicates the object is not linked to another one (i.e. it is a top level object). Omit the third argument, <code>link</code>, to use <code>'parent_id'</code> as the default property which links the object to another one by its <code>id</code>.</p><pre class="language-js"><span class="token keyword">const</span> nest <span class="token operator">=</span> <span class="token punctuation">(</span>items<span class="token punctuation">,</span> id <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">,</span> link <span class="token operator">=</span> <span class="token string">'parent_id'</span><span class="token punctuation">)</span> <span class="token operator">=></span>
|
||||
items
|
||||
<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>item <span class="token operator">=></span> item<span class="token punctuation">[</span>link<span class="token punctuation">]</span> <span class="token operator">===</span> id<span class="token punctuation">)
|
||||
.</span><span class="token function">map</span><span class="token punctuation">(</span>item <span class="token operator">=></span> <span class="token punctuation">({</span> <span class="token operator">...</span>item<span class="token punctuation">,</span> children<span class="token punctuation">:</span> <span class="token function">nest</span><span class="token punctuation">(</span>items<span class="token punctuation">,</span> item<span class="token punctuation">.</span>id<span class="token punctuation">) }));</span>
|
||||
|
||||
Reference in New Issue
Block a user