繁体   English   中英

从 object 的 javascript 数组创建树结构

[英]Create tree structure from javascript array of object

我有一个这样的对象数组..

var obj_1 = {id:1, title:"Title 1", unitId:0, line: 0};
var obj_2 = {id:2, title:"Title 1.1", unitId:1, line: 0};
var obj_3 = {id:3, title:"Title 1.2", unitId:1, line: 1};
var obj_4 = {id:4, title:"Title 1.1.1", unitId:0, line: 1};
var obj_5 = {id:5, title:"Title 2", unitId:0, line: 1};

var obj_list = [obj_1,obj_2,obj_3,obj_4,obj_5];

我想将 json 转换为树结构,我的结果结构是这样的

Unit 0: {
  Line 0: {
     children: {
        {id:1, title:"Title 1", unitId:0, line: 0}
     }
  },
  Line 1: {
     children: {
        {id:4, title:"Title 1.1.1", unitId:0, line: 1},
        {id:5, title:"Title 2", unitId:0, line: 1}
     }
  }
}
Unit 1: {
  Line 0: {
     children: {
        {id:2, title:"Title 1.1", unitId:1, line: 0}
     }
  },
  Line 1: {
     children: {
        {id:2, title:"Title 1.2", unitId:1, line: 2}
     }
  }
}

如果有人知道怎么做,请回答。 提前致谢

您可以使用一组想要的键进行嵌套,并使用此键构建新的 object 并在最后将 object 推送到children属性。

 var data = [{ id: 1, title: "Title 1", unitId: 0, line: 0 }, { id: 2, title: "Title 1.1", unitId: 1, line: 0 }, { id: 3, title: "Title 1.2", unitId: 1, line: 1 }, { id: 4, title: "Title 1.1.1", unitId: 0, line: 1 }, { id: 5, title: "Title 2", unitId: 0, line: 1 }], keys = [['unitId', 'Unit'], ['line', 'Line']], result = data.reduce((r, o) => { var target = keys.reduce((q, [k, t]) => { var key = [t, o[k]].join(' '); return q[key] = q[key] || {}; }, r); (target.children = target.children || []).push(o); return r; }, {}); console.log(result);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

特殊键的字母也是如此。

 var data = [{ id: 1, title: "Title 1", unitId: 0, line: 0 }, { id: 2, title: "Title 1.1", unitId: 1, line: 0 }, { id: 3, title: "Title 1.2", unitId: 1, line: 1 }, { id: 4, title: "Title 1.1.1", unitId: 0, line: 1 }, { id: 5, title: "Title 2", unitId: 0, line: 1 }], keys = [['unitId', 'Unit'], ['line', 'Line', v => (10 + v).toString(36).toUpperCase()]], result = data.reduce((r, o) => { var target = keys.reduce((q, [k, t, fn = v => v]) => { var key = [t, fn(o[k])].join(' '); return q[key] = q[key] || {}; }, r); (target.children = target.children || []).push(o); return r; }, {}); console.log(result);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

第一:得到你想要的结构:

const structure = [];
for (obj of obj_list) {
  if (!structure[obj.unitId]) {
    structure[obj.unitId] = [];
  }
  if (!structure[obj.unitId][obj.line]) {
    structure[obj.unitId][obj.line] = [];
  }
  structure[obj.unitId][obj.line].push(obj);
}

下一个: output 以您想要的格式。 您可能可以处理最后一步。

可能有一种更清洁的方法,只是想快速回答

const obj_1 = { id: 1, title: 'Title 1', unitId: 0, line: 0 };
const obj_2 = { id: 2, title: 'Title 1.1', unitId: 1, line: 0 };
const obj_3 = { id: 3, title: 'Title 1.2', unitId: 1, line: 1 };
const obj_4 = { id: 4, title: 'Title 1.1.1', unitId: 0, line: 1 };
const obj_5 = { id: 5, title: 'Title 2', unitId: 0, line: 1 };

const obj_list = [obj_1, obj_2, obj_3, obj_4, obj_5];

newJson = {};
obj_list.forEach(el => {
    newJson[`unit${el.unitId}`] ? null : (newJson[`unit${el.unitId}`] = {});
    newJson[`unit${el.unitId}`][`line${el.line}`]
        ? null
        : (newJson[`unit${el.unitId}`][`line${el.line}`] = {});
    newJson[`unit${el.unitId}`][`line${el.line}`].children
        ? null
        : (newJson[`unit${el.unitId}`][`line${el.line}`].children = []);

    newJson[`unit${el.unitId}`][`line${el.line}`].children.push(el);
});

console.log(newJson);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM