javascript - Group by each item in an array, using lodash -


i have these kind of data structure api, , told me group them accordingly.

input

{     0: {         id: 0,         name: 'foo',         categories: [             'category001',             'category002/sub-category001'         ]     },     1: {         id: 1,         name: 'bar',         categories: [             'category002/sub-category001'         ]     },     2: {         id: 2,         name: 'bazz',         categories: [             'category001',             'category002',             'category003'         ]     },     3: {         id: 3,         name: 'rem',         categories: [             'category001/sub-category002/nth-category008',             'category001/sub-category004',             'category003/sub-category001'         ]     } } 

desired output

{     0: {         "name": "category001",         "iscategory": true,         "children": [             {                 "id": 0,                 "name": "foo",                 "categorypath": "category001",                 "iscategory": false             },             {                 "id": 2,                 "name": "bazz",                 "categorypath": "category001",                 "iscategory": false             },             {                 "name": "sub-category004",                 "categorypath": "category001/sub-category004",                 "children": [                     {                         "id": 3,                         "name": "rem",                         "iscategory": false,                      }                 ],                 "iscategory": true             },             {                 "name": "sub-category002",                 "categorypath": "category001/sub-category002",                 "children": [                     {                         "name": "sub-category008",                         "categorypath": "category001/sub-category002/nth-category008",                         "children": [                             {                                 "id": 3,                                 "name": "rem",                                 "iscategory": false                             }                         ],                         "iscategory": true                     },                  ],                 "iscategory": true             },             {                 "name": "sub-category002",                 "categorypath": "category001/sub-category002",                 "iscategory": true             }         ],         "categorypath": ""     },     1: {         "name": "category002",         "iscategory": true,         "children": [             {                 "id": 2,                 "name": "bazz",                 "categorypath": "category002",                 "iscategory": false             },             {                 "name": "sub-category001",                 "categorypath": "category002/sub-category001",                 "children": [                     {                         "id": 0,                         "name": "foo",                         "iscategory": false,                      },                     {                         "id": 1,                         "name": "bar",                         "iscategory": false,                      }                 ],                 "iscategory": true             }         ],         "categorypath": ""     },     2: {         "name": "category003",         "iscategory": true,         "children": [             {                 "id": 2,                 "name": "bazz",                 "categorypath": "category002",                 "iscategory": false             },             {                 "name": "sub-category001",                 "categorypath": "category003/sub-category001",                 "children": [                     {                         "id": 0,                         "name": "foo",                         "iscategory": false,                      }                 ],                 "iscategory": true             }         ],         "categorypath": ""     } } 

question

  1. is there easy way of doing in lodash?

a simple groupby won't though, lol

var groups = _.chain(items)         .groupby('categories')         .pairs()         .value(); 

custom processing can not avoided here in addition lodash functions. following attempt use lodash @ most:

var transformed = _(input)     .transform(function (result, item) {         _(item.categories)             .map(function (categorypath) {                 return categorypath.split('/');             })             .each(function (categorypathparts) {                  var dict = result;                 var par;                  var fullpath = _.reduce(categorypathparts, function (path, category, i) {                     path += (i > 0 ? '/' : '') + category;                     if (!(par = _.find(dict, 'name', category))) {                         dict.push(par = {                             name: category,                             categorypath: (i > 0 ? path : ''),                             iscategory: true,                             children: []                         });                     }                      dict = _.find(dict, 'name', category).children;                      return path;             }, "")              par.children.push({                 id: item.id,                 name: item.name,                 iscategory: false,                 categorypath: fullpath,             });         }).value();     }, [])     .transform(function (resobj, rescat, i) {         resobj[i] = rescat;     }, {}); 

var input = {      0: {          id: 0,          name: 'foo',          categories: [              'category001',              'category002/sub-category001']      },      1: {          id: 1,          name: 'bar',          categories: [              'category002/sub-category001']      },      2: {          id: 2,          name: 'bazz',          categories: [              'category001',              'category002',              'category003']      },      3: {          id: 3,          name: 'rem',          categories: [              'category001/sub-category002/nth-category008',              'category001/sub-category004',              'category003/sub-category001']      }  };    var transformed = _(input)      .transform(function (result, item) {          _(item.categories)              .map(function (categorypath) {                  return categorypath.split('/');              })              .each(function (categorypathparts) {                    var dict = result;                  var par;                    var fullpath = _.reduce(categorypathparts, function (path, category, i) {                      path += (i > 0 ? '/' : '') + category;                      if (!(par = _.find(dict, 'name', category))) {                          dict.push(par = {                              name: category,                              categorypath: (i > 0 ? path : ''),                              iscategory: true,                              children: []                          });                      }                        dict = _.find(dict, 'name', category).children;                        return path;              }, "")                par.children.push({                  id: item.id,                  name: item.name,                  iscategory: false,                  categorypath: fullpath,              });          }).value();      }, [])      .transform(function (resobj, rescat, i) {          resobj[i] = rescat;      }, {});    document.getelementbyid('resultarea').textcontent = json.stringify(transformed, null, 2);
textarea {    width: 100%;  }
<script src="http://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.0/lodash.min.js"></script>    <textarea id="resultarea" rows="111" ></textarea>


Comments