From 60dc7a86bc37db36c06509d90b2333a59c5d56be Mon Sep 17 00:00:00 2001 From: 30secondsofcode <30secondsofcode@gmail.com> Date: Mon, 26 Feb 2018 10:25:04 +0000 Subject: [PATCH] Travis build: 1740 --- README.md | 10 ++++------ docs/index.html | 2 +- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 6053c1753..3d0c5191d 100644 --- a/README.md +++ b/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') => diff --git a/docs/index.html b/docs/index.html index 86831a0ac..c5b076685 100644 --- a/docs/index.html +++ b/docs/index.html @@ -1478,7 +1478,7 @@ Foo.prototype: 'foo' }; merge(object, other); // { a: [ { x: 2 }, { y: 4 }, { z: 3 } ], b: [ 1, 2, 3 ], c: 'foo' } -
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.
const nest = (items, id = null, link = 'parent_id') => +
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 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.
const nest = (items, id = null, link = 'parent_id') => items .filter(item => item[link] === id) .map(item => ({ ...item, children: nest(items, item.id) }));